Skip to content

Commit

Permalink
Add integration test for running a suite multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
nicojs committed Apr 22, 2020
1 parent 2da4688 commit 26ec939
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
@@ -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;
}
});
});
@@ -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(']');
});
});
});

@@ -0,0 +1,5 @@
describe('slow suite', () => {
it('should be slow', (done) => {
setTimeout(200, done);
});
});
@@ -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();

74 changes: 74 additions & 0 deletions 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-suite.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-suite.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-suite.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']}
);
});
});

0 comments on commit 26ec939

Please sign in to comment.