diff --git a/test/integration/fixtures/multiple-runs/multiple-runs-different-output-suite.fixture.js b/test/integration/fixtures/multiple-runs/multiple-runs-different-output-suite.fixture.js new file mode 100644 index 0000000000..903f661bf9 --- /dev/null +++ b/test/integration/fixtures/multiple-runs/multiple-runs-different-output-suite.fixture.js @@ -0,0 +1,19 @@ +describe('Multiple runs', () => { + + /** + * Shared state! Bad practice, but nice for this test + */ + let i = 0; + + it('should skip, fail and pass respectively', function () { + switch (i++) { + case 0: + this.skip(); + case 1: + throw new Error('Expected error'); + default: + // this is fine ☕ + break; + } + }); +}); diff --git a/test/integration/fixtures/multiple-runs/multiple-runs-different-output.fixture.js b/test/integration/fixtures/multiple-runs/multiple-runs-different-output.fixture.js new file mode 100644 index 0000000000..01951a7710 --- /dev/null +++ b/test/integration/fixtures/multiple-runs/multiple-runs-different-output.fixture.js @@ -0,0 +1,22 @@ +'use strict'; +const Mocha = require('../../../../lib/mocha'); + +const mocha = new Mocha( { reporter: 'json' } ); +if(process.argv.indexOf('--no-clean-references') >= 0) { + mocha.cleanReferencesAfterRun(false); +} +if(process.argv.indexOf('--directly-dispose') >= 0){ + mocha.dispose(); +} +mocha.addFile(require.resolve('./multiple-runs-different-output-suite.fixture.js')); +console.log('['); +mocha.run(() => { + console.log(','); + mocha.run(() => { + console.log(','); + mocha.run(() => { + console.log(']'); + }); + }); +}); + diff --git a/test/integration/fixtures/multiple-runs/start-second-run-if-previous-is-still-running-suite.fixture.js b/test/integration/fixtures/multiple-runs/start-second-run-if-previous-is-still-running-suite.fixture.js new file mode 100644 index 0000000000..a8ecaf76e5 --- /dev/null +++ b/test/integration/fixtures/multiple-runs/start-second-run-if-previous-is-still-running-suite.fixture.js @@ -0,0 +1,5 @@ +describe('slow suite', () => { + it('should be slow', (done) => { + setTimeout(200, done); + }); +}); diff --git a/test/integration/fixtures/multiple-runs/start-second-run-if-previous-is-still-running.fixture.js b/test/integration/fixtures/multiple-runs/start-second-run-if-previous-is-still-running.fixture.js new file mode 100644 index 0000000000..0cc4db3a92 --- /dev/null +++ b/test/integration/fixtures/multiple-runs/start-second-run-if-previous-is-still-running.fixture.js @@ -0,0 +1,8 @@ +'use strict'; +const Mocha = require('../../../../lib/mocha'); + +const mocha = new Mocha( { reporter: 'json' } ); +mocha.addFile(require.resolve('./start-second-run-if-previous-is-still-running-suite.fixture.js')); +mocha.run(); +mocha.run(); + diff --git a/test/integration/multiple-runs.spec.js b/test/integration/multiple-runs.spec.js new file mode 100644 index 0000000000..ccb0f3c892 --- /dev/null +++ b/test/integration/multiple-runs.spec.js @@ -0,0 +1,74 @@ +'use strict'; + +var invokeNode = require('./helpers').invokeNode; + +describe('multiple runs', function(done) { + it('should should be allowed to run multiple times if cleanReferences is turned off', function(done) { + var path = require.resolve( + './fixtures/multiple-runs/multiple-runs-different-output.fixture.js' + ); + invokeNode([path, '--no-clean-references'], function(err, res) { + expect(err, 'to be null'); + expect(res.code, 'to be', 0); + var results = JSON.parse(res.output); + expect(results, 'to have length', 3); + expect(results[0].pending, 'to have length', 1); + expect(results[0].failures, 'to have length', 0); + expect(results[0].passes, 'to have length', 0); + expect(results[1].pending, 'to have length', 0); + expect(results[1].failures, 'to have length', 1); + expect(results[1].passes, 'to have length', 0); + expect(results[2].pending, 'to have length', 0); + expect(results[2].failures, 'to have length', 0); + expect(results[2].passes, 'to have length', 1); + done(); + }); + }); + + it('should should not be allowed if cleanReferences is true', function(done) { + var path = require.resolve( + './fixtures/multiple-runs/multiple-runs-different-output.fixture.js' + ); + invokeNode( + [path], + function(err, res) { + expect(err, 'to be null'); + expect(res.code, 'not to be', 0); + expect(res.output, 'to contain', 'ERR_MOCHA_INSTANCE_ALREADY_DISPOSED'); + done(); + }, + {stdio: ['ignore', 'pipe', 'pipe']} + ); + }); + + it('should should not be allowed if the instance is disposed', function(done) { + var path = require.resolve( + './fixtures/multiple-runs/multiple-runs-different-output.fixture.js' + ); + invokeNode( + [path, '--directly-dispose'], + function(err, res) { + expect(err, 'to be null'); + expect(res.code, 'not to be', 0); + expect(res.output, 'to contain', 'ERR_MOCHA_INSTANCE_ALREADY_DISPOSED'); + done(); + }, + {stdio: ['ignore', 'pipe', 'pipe']} + ); + }); + + it('should should not allowed to run while a previous run is in progress', function(done) { + var path = require.resolve( + './fixtures/multiple-runs/start-second-run-if-previous-is-still-running.fixture' + ); + invokeNode( + [path], + function(err, res) { + expect(err, 'to be null'); + expect(res.output, 'to contain', 'ERR_MOCHA_INSTANCE_ALREADY_RUNNING'); + done(); + }, + {stdio: ['ignore', 'pipe', 'pipe']} + ); + }); +});