Skip to content

Commit

Permalink
Add --bail support
Browse files Browse the repository at this point in the history
  • Loading branch information
geek committed Jan 17, 2017
1 parent aae0a5a commit c074671
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ global manipulation. Our goal with **lab** is to keep the execution engine as si

**lab** supports the following command line options:
- `-a`, `--assert` - name of assert library to use.
- `--bail` - terminate the process with a non-zero exit code on the first test failure. Defaults to `false`.
- `-c`, `--coverage` - enables code coverage analysis.
- `--coverage-path` - sets code coverage path.
- `--coverage-exclude` - sets code coverage excludes.
Expand Down
10 changes: 8 additions & 2 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ internals.options = function () {
description: 'specify an assertion library module path to require and make available under Lab.assertions',
default: null
},
bail: {
type: 'boolean',
description: 'exit the process with a non zero exit code on the first test failure',
default: null
},
colors: {
alias: 'C',
type: 'boolean',
Expand Down Expand Up @@ -354,7 +359,7 @@ internals.options = function () {
};

const defaults = {
paths: ['test'],
bail: false,
coverage: false,
debug: false,
dry: false,
Expand All @@ -367,6 +372,7 @@ internals.options = function () {
'lint-errors-threshold': 0,
'lint-warnings-threshold': 0,
parallel: false,
paths: ['test'],
rejections: false,
reporter: 'console',
shuffle: false,
Expand Down Expand Up @@ -398,7 +404,7 @@ internals.options = function () {
const options = Utils.mergeOptions(defaults, internals.rc);
options.paths = argv._ ? [].concat(argv._) : options.paths;

const keys = ['assert', 'colors', 'context-timeout', 'coverage', 'coverage-exclude',
const keys = ['assert', 'bail', 'colors', 'context-timeout', 'coverage', 'coverage-exclude',
'coverage-path', 'debug', 'dry', 'environment', 'flat', 'globals', 'grep',
'lint', 'lint-errors-threshold', 'lint-fix', 'lint-options', 'lint-warnings-threshold',
'linter', 'output', 'parallel', 'pattern', 'rejections', 'reporter', 'seed', 'shuffle', 'silence',
Expand Down
15 changes: 15 additions & 0 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Error.stackTraceLimit = Infinity; // Set Error stack size
internals.defaults = {

// assert: { incomplete(), count() },
bail: false,
coverage: false,

// coveragePath: process.cwd(),
Expand Down Expand Up @@ -326,6 +327,10 @@ internals.executeExperiments = function (experiments, state, skip, callback) {
state.report.errors.push(err);
}

if (state.report.errors.length && state.options.bail === true) {
return callback();
}

nextExperiment();
});
},
Expand Down Expand Up @@ -356,6 +361,10 @@ internals.executeTests = function (experiment, state, skip, callback) {
return callback();
}

// Set to an error when options.bail is set and a test fails

let bail = null;

// Collect beforeEach and afterEach from parents

const befores = skip ? [] : internals.collectDeps(experiment, 'beforeEaches');
Expand Down Expand Up @@ -430,12 +439,14 @@ internals.executeTests = function (experiment, state, skip, callback) {
state.report.failures++;
test.err = err;
test.timeout = err.timeout;
bail = state.options.bail === true ? err : null;
}

test.duration = Date.now() - start;

state.report.tests.push(test);
state.reporter.test(test);

return next();
});
},
Expand All @@ -461,6 +472,10 @@ internals.executeTests = function (experiment, state, skip, callback) {
state.report.errors.push(err);
}

if (bail) {
return callback(bail);
}

return nextTest();
});
};
Expand Down
15 changes: 15 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ describe('CLI', () => {
});
});

it('(--bail) exits with code 1 running a directory of tests after one fails', (done) => {

RunCli(['test/cli_bail', '-m', '2000', '--bail'], (error, result) => {

if (error) {
done(error);
}

expect(result.code).to.equal(1);
expect(result.output).to.contain('Expected 1 to equal specified value');
expect(result.output).to.contain('1 of 2 tests failed');
done();
});
});

it('exits with code 1 when function returns error with multiple reporters', (done) => {

RunCli(['test/cli_failure/failure.js', '-r', 'console', '-r', 'lcov'], (error, result) => {
Expand Down
44 changes: 44 additions & 0 deletions test/cli_bail/test1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

// Load modules

const Code = require('code');
const _Lab = require('../../test_runner');


// Declare internals

const internals = {};


// Test shortcuts

const lab = exports.lab = _Lab.script();
const before = lab.before;
const after = lab.after;
const describe = lab.describe;
const it = lab.it;
const expect = Code.expect;


describe('test1', () => {

before((done) => {

process.nextTick(done);
});

it('should add numbers', (done) => {

process.nextTick(() => {

expect(1 + 1).to.equal(2);
done();
});
});

after((done) => {

process.nextTick(done);
});
});
44 changes: 44 additions & 0 deletions test/cli_bail/test2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

// Load modules

const Code = require('code');
const _Lab = require('../../test_runner');


// Declare internals

const internals = {};


// Test shortcuts

const lab = exports.lab = _Lab.script();
const before = lab.before;
const after = lab.after;
const describe = lab.describe;
const it = lab.it;
const expect = Code.expect;


describe('test2', () => {

before((done) => {

process.nextTick(done);
});

it('should multiply numbers', (done) => {

process.nextTick(() => {

expect(1 * 1).to.equal(2);
done();
});
});

after((done) => {

process.nextTick(done);
});
});
44 changes: 44 additions & 0 deletions test/cli_bail/test3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

// Load modules

const Code = require('code');
const _Lab = require('../../test_runner');


// Declare internals

const internals = {};


// Test shortcuts

const lab = exports.lab = _Lab.script();
const before = lab.before;
const after = lab.after;
const describe = lab.describe;
const it = lab.it;
const expect = Code.expect;


describe('test2', () => {

before((done) => {

process.nextTick(done);
});

it('should multiply numbers', (done) => {

process.nextTick(() => {

expect(1 * 1).to.equal(1);
done();
});
});

after((done) => {

process.nextTick(done);
});
});
33 changes: 32 additions & 1 deletion test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,38 @@ describe('Runner', () => {
});
});

it('dry run', (done) => {
it('bail will terminate on the first test failure', (done) => {

const script = Lab.script();
script.experiment('test', () => {

script.test('1', (testDone) => {

testDone();
});

script.test('2', (testDone) => {

throw new Error('bailing');
});

script.test('3', (testDone) => {

testDone();
});
});

Lab.execute(script, { bail: true }, null, (err, notebook) => {

expect(err).not.to.exist();
expect(notebook.tests).to.have.length(2);
expect(notebook.failures).to.equal(1);
expect(notebook.errors[0].message).to.contain('bailing');
done();
});
});

it('dry run won\'t execute tests', (done) => {

const script = Lab.script();
script.experiment('test', () => {
Expand Down

0 comments on commit c074671

Please sign in to comment.