diff --git a/bin/_mocha b/bin/_mocha index 26607f452a..3802e70b79 100755 --- a/bin/_mocha +++ b/bin/_mocha @@ -81,10 +81,7 @@ const list = str => str.split(/ *, */); /** * Parse multiple flag. */ -const collect = (val, memo) => { - memo.push(val); - return memo; -}; +const collect = (val, memo) => memo.concat(val); /** * Hide the cursor. @@ -208,7 +205,7 @@ program .option('--allow-uncaught', 'enable uncaught errors to propagate') .option('--forbid-only', 'causes test marked with only to fail the suite') .option('--forbid-pending', 'causes pending tests and test marked with skip to fail the suite') - .option('--file [file]', 'include a file to be ran during the suite', collect, []); + .option('--file ', 'include a file to be ran during the suite', collect, []); program._name = 'mocha'; diff --git a/docs/index.md b/docs/index.md index 5e7f7c6c58..113a84d7a2 100644 --- a/docs/index.md +++ b/docs/index.md @@ -852,7 +852,7 @@ Specify the "slow" test threshold, defaulting to 75ms. Mocha uses this to highli ### `--file ` -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 effected by any other flags (`--recursive` or `--sort` have no effect). Accepts multiple `--file` flags to include multiple files. +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`. ### `-g, --grep ` diff --git a/test/integration/fixtures/options/file-alpha.fixture.js b/test/integration/fixtures/options/file-alpha.fixture.js index 0792429604..100d49aeb1 100644 --- a/test/integration/fixtures/options/file-alpha.fixture.js +++ b/test/integration/fixtures/options/file-alpha.fixture.js @@ -5,5 +5,9 @@ describe('alpha', function () { if (global.beta !== undefined) { throw new Error('alpha was not executed first'); } + + if (global.theta !== undefined) { + throw new Error('alpha was not executed first'); + } }); }); diff --git a/test/integration/fixtures/options/file-beta.fixture.js b/test/integration/fixtures/options/file-beta.fixture.js index 56da4a6523..ce5dc3c0a8 100644 --- a/test/integration/fixtures/options/file-beta.fixture.js +++ b/test/integration/fixtures/options/file-beta.fixture.js @@ -3,5 +3,9 @@ describe('beta', function () { it('should be executed second', function () { global.beta = 1; + + if (global.theta !== undefined) { + throw new Error('beta was not executed second'); + } }); }); diff --git a/test/integration/fixtures/options/file-theta.fixture.js b/test/integration/fixtures/options/file-theta.fixture.js new file mode 100644 index 0000000000..9d5c7d8c96 --- /dev/null +++ b/test/integration/fixtures/options/file-theta.fixture.js @@ -0,0 +1,7 @@ +'use strict'; + +describe('theta', function () { + it('should be executed third', function () { + global.theta = 1; + }); +}); diff --git a/test/integration/options.spec.js b/test/integration/options.spec.js index c19b7af388..607af6083b 100644 --- a/test/integration/options.spec.js +++ b/test/integration/options.spec.js @@ -94,11 +94,9 @@ describe('options', function () { }); describe('--file', function () { - beforeEach(function () { + it('should run tests passed via file first', function (done) { args = ['--file', resolvePath('options/file-alpha.fixture.js')]; - }); - it('should run tests passed via file first', function (done) { run('options/file-beta.fixture.js', args, function (err, res) { if (err) { done(err); @@ -114,6 +112,32 @@ describe('options', function () { done(); }); }); + + it('should run multiple tests passed via file first', function (done) { + args = [ + '--file', resolvePath('options/file-alpha.fixture.js'), + '--file', resolvePath('options/file-beta.fixture.js') + ]; + + run('options/file-theta.fixture.js', args, function (err, res) { + if (err) { + done(err); + return; + } + assert.equal(res.stats.pending, 0); + assert.equal(res.stats.passes, 3); + assert.equal(res.stats.failures, 0); + + assert.equal(res.passes[0].fullTitle, + 'alpha should be executed first'); + assert.equal(res.passes[1].fullTitle, + 'beta should be executed second'); + assert.equal(res.passes[2].fullTitle, + 'theta should be executed third'); + assert.equal(res.code, 0); + done(); + }); + }); }); describe('--delay', function () {