Browse Source

Change the current working directory of tests to be the same directory as package.json (#1074)

#32
hoisting-throws-helper
Martin Grünbaum 8 years ago
committed by Sindre Sorhus
parent
commit
476c653961
  1. 1
      cli.js
  2. 2
      lib/fork.js
  3. 2
      readme.md
  4. 23
      test/api.js
  5. 9
      test/fixture/process-cwd-default.js
  6. 0
      test/fixture/process-cwd-pkgdir.js

1
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
});

2
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

2
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

23
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);
});

9
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);
});

0
test/fixture/process-cwd.js → test/fixture/process-cwd-pkgdir.js

Loading…
Cancel
Save