Skip to content

Commit

Permalink
Change the current working directory of tests to be the same director…
Browse files Browse the repository at this point in the history
…y as package.json (#1074)

#32
  • Loading branch information
alathon authored and sindresorhus committed Oct 18, 2016
1 parent 1b214be commit 476c653
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
Expand Down
2 changes: 1 addition & 1 deletion lib/fork.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 20 additions & 3 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
});

Expand Down Expand Up @@ -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);
});

Expand Down Expand Up @@ -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);
});
Expand Down
9 changes: 9 additions & 0 deletions test/fixture/process-cwd-default.js
Original file line number Diff line number Diff line change
@@ -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);
});
File renamed without changes.

0 comments on commit 476c653

Please sign in to comment.