Skip to content

Commit 4bc021e

Browse files
alathonsindresorhus
authored andcommitted
Change the current working directory of tests to be the same directory as package.json (#1074)
#32
1 parent 1b214be commit 4bc021e

File tree

6 files changed

+32
-5
lines changed

6 files changed

+32
-5
lines changed

cli.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ var api = new Api({
138138
match: arrify(cli.flags.match),
139139
babelConfig: conf.babel,
140140
resolveTestsFrom: cli.input.length === 0 ? pkgDir : process.cwd(),
141+
pkgDir: pkgDir,
141142
timeout: cli.flags.timeout,
142143
concurrency: cli.flags.concurrency ? parseInt(cli.flags.concurrency, 10) : 0
143144
});

lib/fork.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ module.exports = function (file, opts, execArgv) {
4141
}, opts);
4242

4343
var ps = childProcess.fork(path.join(__dirname, 'test-worker.js'), [JSON.stringify(opts)], {
44-
cwd: path.dirname(file),
44+
cwd: opts.pkgDir,
4545
silent: true,
4646
env: env,
4747
execArgv: execArgv || process.execArgv

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ If you're unable to use promises or observables, you may enable "callback mode"
247247

248248
You must define all tests synchronously. They can't be defined inside `setTimeout`, `setImmediate`, etc.
249249

250-
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')`.
250+
AVA tries to run test files with their current working directory set to the directory that contains your `package.json` file.
251251

252252
### Creating tests
253253

test/api.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ var fs = require('fs');
44
var figures = require('figures');
55
var rimraf = require('rimraf');
66
var test = require('tap').test;
7+
var pkgConf = require('pkg-conf');
78
var Api = require('../api');
89
var testCapitalizerPlugin = require('./fixture/babel-plugin-test-capitalizer');
910

11+
var conf = pkgConf.sync('ava');
12+
var pkgDir = path.dirname(pkgConf.filepath(conf));
13+
1014
test('must be called with new', function (t) {
1115
t.throws(function () {
1216
var api = Api;
@@ -18,6 +22,7 @@ test('must be called with new', function (t) {
1822
generateTests('Without Pool: ', function (options) {
1923
options = options || {};
2024
options.powerAssert = true;
25+
options.pkgDir = options.pkgDir || pkgDir;
2126
return new Api(options);
2227
});
2328

@@ -63,6 +68,7 @@ generateTests('With Pool: ', function (options) {
6368
options = options || {};
6469
options.concurrency = 2;
6570
options.powerAssert = true;
71+
options.pkgDir = options.pkgDir || pkgDir;
6672
return new Api(options);
6773
});
6874

@@ -307,12 +313,23 @@ function generateTests(prefix, apiCreator) {
307313
});
308314
});
309315

310-
test(prefix + 'change process.cwd() to a test\'s directory', function (t) {
316+
test(prefix + 'run from package.json folder by default', function (t) {
311317
t.plan(1);
312-
313318
var api = apiCreator();
314319

315-
return api.run([path.join(__dirname, 'fixture/process-cwd.js')])
320+
return api.run([path.join(__dirname, 'fixture/process-cwd-default.js')])
321+
.then(function (result) {
322+
t.is(result.passCount, 1);
323+
});
324+
});
325+
326+
test(prefix + 'change process.cwd() to a test\'s directory with pkgDir', function (t) {
327+
t.plan(1);
328+
329+
var fullPath = path.join(__dirname, 'fixture/process-cwd-pkgdir.js');
330+
var api = apiCreator({pkgDir: path.dirname(fullPath)});
331+
332+
return api.run([fullPath])
316333
.then(function (result) {
317334
t.is(result.passCount, 1);
318335
});

test/fixture/process-cwd-default.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import path from 'path';
2+
import pkgConf from 'pkg-conf';
3+
import test from '../../';
4+
5+
test(t => {
6+
const conf = pkgConf.sync('ava');
7+
const pkgDir = path.dirname(pkgConf.filepath(conf));
8+
t.is(process.cwd(), pkgDir);
9+
});
File renamed without changes.

0 commit comments

Comments
 (0)