diff --git a/cli.js b/cli.js index 22bcb26..38f7f61 100755 --- a/cli.js +++ b/cli.js @@ -138,6 +138,7 @@ var api = new Api({ match: arrify(cli.flags.match), babelConfig: conf.babel, resolveTestsFrom: cli.input.length === 0 ? pkgDir : process.cwd(), + pkgDir: pkgDir, timeout: cli.flags.timeout, concurrency: cli.flags.concurrency ? parseInt(cli.flags.concurrency, 10) : 0 }); diff --git a/lib/fork.js b/lib/fork.js index 7748c69..495f8d8 100644 --- a/lib/fork.js +++ b/lib/fork.js @@ -41,7 +41,7 @@ module.exports = function (file, opts, execArgv) { }, opts); var ps = childProcess.fork(path.join(__dirname, 'test-worker.js'), [JSON.stringify(opts)], { - cwd: path.dirname(file), + cwd: opts.pkgDir, silent: true, env: env, execArgv: execArgv || process.execArgv diff --git a/readme.md b/readme.md index 69768c2..b1d4f51 100644 --- a/readme.md +++ b/readme.md @@ -247,7 +247,7 @@ If you're unable to use promises or observables, you may enable "callback mode" You must define all tests synchronously. They can't be defined inside `setTimeout`, `setImmediate`, etc. -Test files are run from their current directory, so [`process.cwd()`](https://nodejs.org/api/process.html#process_process_cwd) is always the same as [`__dirname`](https://nodejs.org/api/globals.html#globals_dirname). You can just use relative paths instead of doing `path.join(__dirname, 'relative/path')`. +AVA tries to run test files with their current working directory set to the directory that contains your `package.json` file. ### Creating tests diff --git a/test/api.js b/test/api.js index 69dd3e8..e40e7d5 100644 --- a/test/api.js +++ b/test/api.js @@ -4,9 +4,13 @@ var fs = require('fs'); var figures = require('figures'); var rimraf = require('rimraf'); var test = require('tap').test; +var pkgConf = require('pkg-conf'); var Api = require('../api'); var testCapitalizerPlugin = require('./fixture/babel-plugin-test-capitalizer'); +var conf = pkgConf.sync('ava'); +var pkgDir = path.dirname(pkgConf.filepath(conf)); + test('must be called with new', function (t) { t.throws(function () { var api = Api; @@ -18,6 +22,7 @@ test('must be called with new', function (t) { generateTests('Without Pool: ', function (options) { options = options || {}; options.powerAssert = true; + options.pkgDir = options.pkgDir || pkgDir; return new Api(options); }); @@ -63,6 +68,7 @@ generateTests('With Pool: ', function (options) { options = options || {}; options.concurrency = 2; options.powerAssert = true; + options.pkgDir = options.pkgDir || pkgDir; return new Api(options); }); @@ -307,12 +313,23 @@ function generateTests(prefix, apiCreator) { }); }); - test(prefix + 'change process.cwd() to a test\'s directory', function (t) { + test(prefix + 'run from package.json folder by default', function (t) { t.plan(1); - var api = apiCreator(); - return api.run([path.join(__dirname, 'fixture/process-cwd.js')]) + return api.run([path.join(__dirname, 'fixture/process-cwd-default.js')]) + .then(function (result) { + t.is(result.passCount, 1); + }); + }); + + test(prefix + 'change process.cwd() to a test\'s directory with pkgDir', function (t) { + t.plan(1); + + var fullPath = path.join(__dirname, 'fixture/process-cwd-pkgdir.js'); + var api = apiCreator({pkgDir: path.dirname(fullPath)}); + + return api.run([fullPath]) .then(function (result) { t.is(result.passCount, 1); }); diff --git a/test/fixture/process-cwd-default.js b/test/fixture/process-cwd-default.js new file mode 100644 index 0000000..78c4eb4 --- /dev/null +++ b/test/fixture/process-cwd-default.js @@ -0,0 +1,9 @@ +import path from 'path'; +import pkgConf from 'pkg-conf'; +import test from '../../'; + +test(t => { + const conf = pkgConf.sync('ava'); + const pkgDir = path.dirname(pkgConf.filepath(conf)); + t.is(process.cwd(), pkgDir); +}); diff --git a/test/fixture/process-cwd.js b/test/fixture/process-cwd-pkgdir.js similarity index 100% rename from test/fixture/process-cwd.js rename to test/fixture/process-cwd-pkgdir.js