diff --git a/lib/cli/cli.js b/lib/cli/cli.js index af0e7841d6..59fd2f4af4 100755 --- a/lib/cli/cli.js +++ b/lib/cli/cli.js @@ -19,6 +19,7 @@ const {loadOptions, YARGS_PARSER_CONFIG} = require('./options'); const commands = require('./commands'); const ansi = require('ansi-colors'); const {repository, homepage, version, gitter} = require('../../package.json'); +const {cwd} = require('../utils'); /** * - Accepts an `Array` of arguments @@ -31,7 +32,7 @@ exports.main = (argv = process.argv.slice(2)) => { debug('entered main with raw args', argv); // ensure we can require() from current working directory if (typeof module.paths !== 'undefined') { - module.paths.push(process.cwd(), path.resolve('node_modules')); + module.paths.push(cwd(), path.resolve('node_modules')); } Error.stackTraceLimit = Infinity; // configurable via --stack-trace-limit? diff --git a/lib/cli/config.js b/lib/cli/config.js index 932d6c45f4..1fa1e0555e 100644 --- a/lib/cli/config.js +++ b/lib/cli/config.js @@ -11,6 +11,7 @@ const fs = require('fs'); const path = require('path'); const debug = require('debug')('mocha:cli:config'); const findUp = require('find-up'); +const utils = require('../utils'); /** * These are the valid config files, in order of precedence; @@ -92,7 +93,7 @@ exports.loadConfig = filepath => { * @param {string} [cwd] - Current working directory * @returns {string|null} Filepath to config, if found */ -exports.findConfig = (cwd = process.cwd()) => { +exports.findConfig = (cwd = utils.cwd()) => { const filepath = findUp.sync(exports.CONFIG_FILES, {cwd}); if (filepath) { debug('findConfig: found config file %s', filepath); diff --git a/lib/mocha.js b/lib/mocha.js index 5471c80f6b..6817ff880b 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -58,7 +58,7 @@ var mochaStates = utils.defineConstants({ */ if (!process.browser && typeof module.paths !== 'undefined') { - var cwd = process.cwd(); + var cwd = utils.cwd(); module.paths.push(cwd, path.join(cwd, 'node_modules')); } @@ -236,7 +236,7 @@ Mocha.prototype.reporter = function(reporter, reporterOptions) { ) { // Try to load reporters from a path (absolute or relative) try { - _reporter = require(path.resolve(reporter)); + _reporter = require(path.resolve(utils.cwd(), reporter)); } catch (_err) { _err.code === 'MODULE_NOT_FOUND' || _err.message.indexOf('Cannot find module') >= 0 diff --git a/lib/utils.js b/lib/utils.js index 6ab1277409..4e6b194249 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -637,7 +637,7 @@ exports.stackTraceFilter = function() { var slash = path.sep; var cwd; if (is.node) { - cwd = process.cwd() + slash; + cwd = exports.cwd() + slash; } else { cwd = (typeof location === 'undefined' ? window.location @@ -821,3 +821,13 @@ exports.supportsEsModules = function() { } } }; + +/** + * Returns current working directory + * + * Wrapper around `process.cwd()` for isolation + * @private + */ +exports.cwd = function cwd() { + return process.cwd(); +}; diff --git a/test/node-unit/mocha.spec.js b/test/node-unit/mocha.spec.js index f716f7e8ea..2b9c99a59a 100644 --- a/test/node-unit/mocha.spec.js +++ b/test/node-unit/mocha.spec.js @@ -26,7 +26,8 @@ describe('Mocha', function() { supportsEsModules: sandbox.stub().returns(false), warn: sandbox.stub(), isString: sandbox.stub(), - noop: sandbox.stub() + noop: sandbox.stub(), + cwd: sandbox.stub().returns(process.cwd()) }; stubs.suite = Object.assign(sandbox.createStubInstance(EventEmitter), { slow: sandbox.stub(), @@ -106,10 +107,9 @@ describe('Mocha', function() { describe('reporter()', function() { describe('when a reporter exists relative to the cwd', function() { beforeEach(function() { - // DANGER! - sandbox - .stub(process, 'cwd') - .returns(path.resolve(__dirname, '..', '..', 'lib', 'reporters')); + stubs.utils.cwd.returns( + path.resolve(__dirname, '..', '..', 'lib', 'reporters') + ); }); it('should load from current working directory', function() { diff --git a/test/node-unit/utils.spec.js b/test/node-unit/utils.spec.js new file mode 100644 index 0000000000..c0b6294450 --- /dev/null +++ b/test/node-unit/utils.spec.js @@ -0,0 +1,20 @@ +'use strict'; + +const rewiremock = require('rewiremock/node'); + +describe('utils', function() { + let utils; + + beforeEach(function() { + // add deps to be mocked as needed to second parameter + utils = rewiremock.proxy('../../lib/utils', {}); + }); + + describe('function', function() { + describe('cwd()', function() { + it('should return the current working directory', function() { + expect(utils.cwd(), 'to be', process.cwd()); + }); + }); + }); +});