Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit 77cc72d

Browse files
hswolffboneskull
authored andcommitted
Add ability to pass in test files to be ran before positional files via --file (mochajs#3190)
* Add ability to pass in test files to be ran before positional files via --file Fixes mochajs#3181
1 parent 3c546df commit 77cc72d

File tree

7 files changed

+104
-5
lines changed

7 files changed

+104
-5
lines changed

bin/_mocha

+11-2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ const exit = code => {
7878
*/
7979
const list = str => str.split(/ *, */);
8080

81+
/**
82+
* Parse multiple flag.
83+
*/
84+
const collect = (val, memo) => memo.concat(val);
85+
8186
/**
8287
* Hide the cursor.
8388
*/
@@ -199,7 +204,8 @@ program
199204
.option('--delay', 'wait for async suite definition')
200205
.option('--allow-uncaught', 'enable uncaught errors to propagate')
201206
.option('--forbid-only', 'causes test marked with only to fail the suite')
202-
.option('--forbid-pending', 'causes pending tests and test marked with skip to fail the suite');
207+
.option('--forbid-pending', 'causes pending tests and test marked with skip to fail the suite')
208+
.option('--file <file>', 'include a file to be ran during the suite', collect, []);
203209

204210
program._name = 'mocha';
205211

@@ -497,13 +503,16 @@ if (!files.length) {
497503
}
498504

499505
// resolve
500-
506+
let fileArgs = program.file.map(path => resolve(path));
501507
files = files.map(path => resolve(path));
502508

503509
if (program.sort) {
504510
files.sort();
505511
}
506512

513+
// add files given through --file to be ran first
514+
files = fileArgs.concat(files);
515+
507516
// --watch
508517

509518
let runner;

docs/index.md

+5
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,7 @@ Mocha supports the `err.expected` and `err.actual` properties of any thrown `Ass
736736
--debug-brk enable node's debugger breaking on the first line
737737
--globals <names> allow the given comma-delimited global [names]
738738
--es_staging enable all staged features
739+
--file <file> include a file to be ran during the suite [file]
739740
--harmony<_classes,_generators,...> all node --harmony* flags are available
740741
--preserve-symlinks Instructs the module loader to preserve symbolic links when resolving and caching modules
741742
--icu-data-dir include ICU data
@@ -853,6 +854,10 @@ Disables timeouts. Equivalent to `--timeout 0`.
853854

854855
Specify the "slow" test threshold, defaulting to 75ms. Mocha uses this to highlight test-cases that are taking too long.
855856

857+
### `--file <file>`
858+
859+
Add a file you want included first in a test suite. This is useful if you have some generic setup code that must be included within the test suite. The file passed is not affected by any other flags (`--recursive` or `--sort` have no effect). Accepts multiple `--file` flags to include multiple files, the order in which the flags are given are the order in which the files are included in the test suite. Can also be used in `mocha.opts`.
860+
856861
### `-g, --grep <pattern>`
857862

858863
The `--grep` option when specified will trigger mocha to only run tests matching the given `pattern` which is internally compiled to a `RegExp`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
describe('alpha', function () {
4+
it('should be executed first', function () {
5+
if (global.beta !== undefined) {
6+
throw new Error('alpha was not executed first');
7+
}
8+
9+
if (global.theta !== undefined) {
10+
throw new Error('alpha was not executed first');
11+
}
12+
});
13+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
describe('beta', function () {
4+
it('should be executed second', function () {
5+
global.beta = 1;
6+
7+
if (global.theta !== undefined) {
8+
throw new Error('beta was not executed second');
9+
}
10+
});
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
describe('theta', function () {
4+
it('should be executed third', function () {
5+
global.theta = 1;
6+
});
7+
});

test/integration/helpers.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,12 @@ module.exports = {
129129
* @param {Function} done - Callback
130130
* @param {string} cwd - Current working directory for mocha run, optional
131131
*/
132-
invokeMocha: invokeMocha
132+
invokeMocha: invokeMocha,
133+
134+
/**
135+
* Resolves the path to a fixture to the full path.
136+
*/
137+
resolveFixturePath: resolveFixturePath
133138
};
134139

135140
function invokeMocha (args, fn, cwd) {

test/integration/options.spec.js

+51-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
var path = require('path');
44
var assert = require('assert');
5-
var run = require('./helpers').runMochaJSON;
6-
var directInvoke = require('./helpers').invokeMocha;
5+
var helpers = require('./helpers');
6+
var run = helpers.runMochaJSON;
7+
var directInvoke = helpers.invokeMocha;
8+
var resolvePath = helpers.resolveFixturePath;
79
var args = [];
810

911
describe('options', function () {
@@ -91,6 +93,53 @@ describe('options', function () {
9193
});
9294
});
9395

96+
describe('--file', function () {
97+
it('should run tests passed via file first', function (done) {
98+
args = ['--file', resolvePath('options/file-alpha.fixture.js')];
99+
100+
run('options/file-beta.fixture.js', args, function (err, res) {
101+
if (err) {
102+
done(err);
103+
return;
104+
}
105+
assert.equal(res.stats.pending, 0);
106+
assert.equal(res.stats.passes, 2);
107+
assert.equal(res.stats.failures, 0);
108+
109+
assert.equal(res.passes[0].fullTitle,
110+
'alpha should be executed first');
111+
assert.equal(res.code, 0);
112+
done();
113+
});
114+
});
115+
116+
it('should run multiple tests passed via file first', function (done) {
117+
args = [
118+
'--file', resolvePath('options/file-alpha.fixture.js'),
119+
'--file', resolvePath('options/file-beta.fixture.js')
120+
];
121+
122+
run('options/file-theta.fixture.js', args, function (err, res) {
123+
if (err) {
124+
done(err);
125+
return;
126+
}
127+
assert.equal(res.stats.pending, 0);
128+
assert.equal(res.stats.passes, 3);
129+
assert.equal(res.stats.failures, 0);
130+
131+
assert.equal(res.passes[0].fullTitle,
132+
'alpha should be executed first');
133+
assert.equal(res.passes[1].fullTitle,
134+
'beta should be executed second');
135+
assert.equal(res.passes[2].fullTitle,
136+
'theta should be executed third');
137+
assert.equal(res.code, 0);
138+
done();
139+
});
140+
});
141+
});
142+
94143
describe('--delay', function () {
95144
before(function () {
96145
args = ['--delay'];

0 commit comments

Comments
 (0)