diff --git a/lib/fork.js b/lib/fork.js index b9bc5cfaf..3affd0795 100644 --- a/lib/fork.js +++ b/lib/fork.js @@ -7,6 +7,21 @@ var debug = require('debug')('ava'); var AvaError = require('./ava-error'); var send = require('./send'); +var env = process.env; + +if (process.env.NODE_PATH) { + var nodePath = process.env.NODE_PATH + .split(path.delimiter) + .map(function (p) { + return path.resolve(p); + }) + .join(path.delimiter); + + env = objectAssign({ + NODE_PATH: nodePath + }, env); +} + module.exports = function (file, opts) { opts = objectAssign({ file: file, @@ -18,7 +33,8 @@ module.exports = function (file, opts) { var ps = childProcess.fork(path.join(__dirname, 'test-worker.js'), [JSON.stringify(opts)], { cwd: path.dirname(file), - silent: true + silent: true, + env: env }); var relFile = path.relative('.', file); diff --git a/test/cli.js b/test/cli.js index b63932a83..555f8b0c7 100644 --- a/test/cli.js +++ b/test/cli.js @@ -8,16 +8,19 @@ var arrify = require('arrify'); var touch = require('touch'); var cliPath = path.join(__dirname, '../cli.js'); -function execCli(args, dirname, cb) { - if (typeof dirname === 'function') { - cb = dirname; +function execCli(args, opts, cb) { + var dirname; + var env; + + if (typeof opts === 'function') { + cb = opts; dirname = __dirname; + env = {}; } else { - dirname = path.join(__dirname, dirname); + dirname = path.join(__dirname, opts.dirname ? opts.dirname : ''); + env = opts.env || {}; } - var env = {}; - if (process.env.AVA_APPVEYOR) { env.AVA_APPVEYOR = 1; } @@ -95,21 +98,21 @@ test('log failed tests', function (t) { }); test('pkg-conf: defaults', function (t) { - execCli([], 'fixture/pkg-conf/defaults', function (err) { + execCli([], {dirname: 'fixture/pkg-conf/defaults'}, function (err) { t.ifError(err); t.end(); }); }); test('pkg-conf: pkg-overrides', function (t) { - execCli([], 'fixture/pkg-conf/pkg-overrides', function (err) { + execCli([], {dirname: 'fixture/pkg-conf/pkg-overrides'}, function (err) { t.ifError(err); t.end(); }); }); test('pkg-conf: cli takes precedence', function (t) { - execCli(['--no-serial', '--cache', '--no-fail-fast', '--require=./required.js', 'c.js'], 'fixture/pkg-conf/precedence', function (err) { + execCli(['--no-serial', '--cache', '--no-fail-fast', '--require=./required.js', 'c.js'], {dirname: 'fixture/pkg-conf/precedence'}, function (err) { t.ifError(err); t.end(); }); @@ -124,7 +127,7 @@ test('watcher works', function (t) { hasChokidar = true; } catch (err) {} - var child = execCli(['--verbose', '--watch', 'test.js'], 'fixture/watcher', function (err, stdout) { + var child = execCli(['--verbose', '--watch', 'test.js'], {dirname: 'fixture/watcher'}, function (err, stdout) { if (err && err.code === 1 && !hasChokidar) { t.comment('chokidar dependency is missing, cannot test watcher'); t.match(stdout, 'The optional dependency chokidar failed to install and is required for --watch. Chokidar is likely not supported on your platform.'); @@ -155,3 +158,11 @@ test('watcher works', function (t) { } }); }); + +test('handles NODE_PATH', function (t) { + var nodePaths = 'node-paths/modules' + path.delimiter + 'node-paths/deep/nested'; + execCli('fixture/node-paths.js', {env: {NODE_PATH: nodePaths}}, function (err) { + t.notOk(err); + t.end(); + }); +}); diff --git a/test/fixture/node-paths.js b/test/fixture/node-paths.js new file mode 100644 index 000000000..a199611e7 --- /dev/null +++ b/test/fixture/node-paths.js @@ -0,0 +1,9 @@ +import test from '../../'; + +import foo from 'nested/foo'; +import bar from 'path/bar'; + +test('relative require', t => { + t.is(foo(), 'bar'); + t.is(bar(), 'baz'); +}); diff --git a/test/fixture/node-paths/deep/nested/path/bar.js b/test/fixture/node-paths/deep/nested/path/bar.js new file mode 100644 index 000000000..9642d6cc4 --- /dev/null +++ b/test/fixture/node-paths/deep/nested/path/bar.js @@ -0,0 +1,4 @@ +module.exports = function () { + return 'baz'; +}; + diff --git a/test/fixture/node-paths/modules/nested/foo.js b/test/fixture/node-paths/modules/nested/foo.js new file mode 100644 index 000000000..a695c3b37 --- /dev/null +++ b/test/fixture/node-paths/modules/nested/foo.js @@ -0,0 +1,4 @@ +module.exports = function () { + return 'bar'; +}; +