From c148fbc3139c229a252ebaf5ea5f52910360120f Mon Sep 17 00:00:00 2001 From: Christopher Hiller Date: Mon, 28 Jan 2019 11:26:07 -0800 Subject: [PATCH] ensure --debug becomes --inspect; closes #3697 - add some handiness to the integration test helpers - update docstrings in integration test helpers Signed-off-by: Christopher Hiller --- bin/mocha | 3 +- test/assertions.js | 17 ++---- .../fixtures/__default__.fixture.js | 9 ++++ test/integration/helpers.js | 53 ++++++++++++++++--- test/integration/options/debug.spec.js | 24 +++++++++ 5 files changed, 87 insertions(+), 19 deletions(-) create mode 100644 test/integration/fixtures/__default__.fixture.js create mode 100644 test/integration/options/debug.spec.js diff --git a/bin/mocha b/bin/mocha index 770fb265eb..bc61a54c02 100755 --- a/bin/mocha +++ b/bin/mocha @@ -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'); diff --git a/test/assertions.js b/test/assertions.js index 590219b8a5..08f7885074 100644 --- a/test/assertions.js +++ b/test/assertions.js @@ -59,9 +59,9 @@ exports.mixinMochaAssertions = function(expect) { } ) .addAssertion( - ' [not] to have completed with [exit] code ', + ' [not] to have [completed with] [exit] code ', function(expect, result, code) { - expect(result.code, '[not] to be', code); + expect(result, '[not] to have property', 'code', code); } ) .addAssertion( @@ -290,17 +290,10 @@ exports.mixinMochaAssertions = function(expect) { }); } ) - .addAssertion(' [not] to contain output ', function( - expect, - result, - output - ) { - expect(result.output, '[not] to satisfy', output); - }) .addAssertion( - ' to have [exit] code ', - function(expect, result, code) { - expect(result.code, 'to be', code); + ' [not] to contain [output] ', + function(expect, result, output) { + expect(result.output, '[not] to satisfy', output); } ); }; diff --git a/test/integration/fixtures/__default__.fixture.js b/test/integration/fixtures/__default__.fixture.js new file mode 100644 index 0000000000..58baa12f4c --- /dev/null +++ b/test/integration/fixtures/__default__.fixture.js @@ -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(); + }); +}); diff --git a/test/integration/helpers.js b/test/integration/helpers.js index 0680faa017..760ae41b90 100644 --- a/test/integration/helpers.js +++ b/test/integration/helpers.js @@ -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 @@ -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(), diff --git a/test/integration/options/debug.spec.js b/test/integration/options/debug.spec.js new file mode 100644 index 0000000000..a6655f45bf --- /dev/null +++ b/test/integration/options/debug.spec.js @@ -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'} + ); + }); +});