Skip to content

Commit

Permalink
ensure --debug becomes --inspect; closes #3697
Browse files Browse the repository at this point in the history
- add some handiness to the integration test helpers
- update docstrings in integration test helpers

Signed-off-by: Christopher Hiller <[email protected]>
  • Loading branch information
boneskull committed Jan 28, 2019
1 parent 1f36ec5 commit c148fbc
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 19 deletions.
3 changes: 2 additions & 1 deletion bin/mocha
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ Object.keys(opts).forEach(opt => {
}
});

// allow --debug to invoke --inspect on Node.js v8 or newer nodeOpts.inspect = childOpts.debug;
// allow --debug to invoke --inspect on Node.js v8 or newer
if (childOpts.debug) {
nodeOpts.inspect = childOpts.debug;
childOpts.timeout = false;
delete childOpts.debug;
debug('--debug -> --inspect');
Expand Down
17 changes: 5 additions & 12 deletions test/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ exports.mixinMochaAssertions = function(expect) {
}
)
.addAssertion(
'<RawRunResult|JSONRunResult> [not] to have completed with [exit] code <number>',
'<RawResult|RawRunResult|JSONRunResult> [not] to have [completed with] [exit] code <number>',
function(expect, result, code) {
expect(result.code, '[not] to be', code);
expect(result, '[not] to have property', 'code', code);
}
)
.addAssertion(
Expand Down Expand Up @@ -290,17 +290,10 @@ exports.mixinMochaAssertions = function(expect) {
});
}
)
.addAssertion('<RawRunResult> [not] to contain output <any>', function(
expect,
result,
output
) {
expect(result.output, '[not] to satisfy', output);
})
.addAssertion(
'<RawRunResult|JSONRunResult> to have [exit] code <number>',
function(expect, result, code) {
expect(result.code, 'to be', code);
'<RawResult|RawRunResult> [not] to contain [output] <any>',
function(expect, result, output) {
expect(result.output, '[not] to satisfy', output);
}
);
};
9 changes: 9 additions & 0 deletions test/integration/fixtures/__default__.fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// this generic fixture does nothing special and will be used if no fixture is supplied

'use strict';

describe('a suite', function() {
it('should succeed', function(done) {
done();
});
});
53 changes: 47 additions & 6 deletions test/integration/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ var spawn = require('cross-spawn').spawn;
var path = require('path');
var baseReporter = require('../../lib/reporters/base');

var DEFAULT_FIXTURE = resolveFixturePath('__default__');
var MOCHA_EXECUTABLE = require.resolve('../../bin/mocha');
var _MOCHA_EXECUTABLE = require.resolve('../../bin/_mocha');

module.exports = {
DEFAULT_FIXTURE: DEFAULT_FIXTURE,
/**
* Invokes the mocha binary for the given fixture with color output disabled.
* Accepts an array of additional command line args to pass. The callback is
Expand Down Expand Up @@ -177,20 +182,56 @@ function toJSONRunResult(result) {
return result;
}

function invokeMocha(args, fn, opts) {
args = [path.join(__dirname, '..', '..', 'bin', 'mocha')].concat(args);
/**
* Creates arguments loading a default fixture if none provided
*
* @param {string[]|*} [args] - Arguments to `spawn`
* @returns string[]
*/
function defaultArgs(args) {
return !args || !args.length ? ['--file', DEFAULT_FIXTURE] : args;
}

return _spawnMochaWithListeners(args, fn, opts);
function invokeMocha(args, fn, opts) {
if (typeof args === 'function') {
opts = fn;
fn = args;
args = [];
}
return _spawnMochaWithListeners(
defaultArgs([MOCHA_EXECUTABLE].concat(args)),
fn,
opts
);
}

function invokeSubMocha(args, fn, opts) {
args = [path.join(__dirname, '..', '..', 'bin', '_mocha')].concat(args);

return _spawnMochaWithListeners(args, fn, opts);
if (typeof args === 'function') {
opts = fn;
fn = args;
args = [];
}
return _spawnMochaWithListeners(
defaultArgs([_MOCHA_EXECUTABLE].concat(args)),
fn,
opts
);
}

/**
* Spawns Mocha in a subprocess and returns an object containing its output and exit code
*
* @param {string[]} args - Path to executable and arguments
* @param {Function} fn - Callback
* @param {Object|string} [opts] - Options to `cross-spawn`, or 'pipe' for shortcut to `{stdio: pipe}`
* @returns {ChildProcess}
* @private
*/
function _spawnMochaWithListeners(args, fn, opts) {
var output = '';
if (opts === 'pipe') {
opts = {stdio: 'pipe'};
}
opts = Object.assign(
{
cwd: process.cwd(),
Expand Down
24 changes: 24 additions & 0 deletions test/integration/options/debug.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

var helpers = require('../helpers');
var invokeMocha = helpers.invokeMocha;
var DEFAULT_FIXTURE = helpers.DEFAULT_FIXTURE;

describe('--debug', function() {
it('should invoke --inspect', function(done) {
invokeMocha(
['--debug', '--file', DEFAULT_FIXTURE],
function(err, res) {
if (err) {
return done(err);
}
expect(res, 'to have passed').and(
'to contain output',
/Debugger listening/i
);
done();
},
{stdio: 'pipe'}
);
});
});

0 comments on commit c148fbc

Please sign in to comment.