Skip to content

Make AVA display subdirs properly and be recursive by default. #373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var Promise = require('bluebird');
var figures = require('figures');
var globby = require('globby');
var chalk = require('chalk');
var commondir = require('commondir');
var resolveCwd = require('resolve-cwd');
var fork = require('./lib/fork');
var formatter = require('./lib/enhance-assert').formatter();
Expand All @@ -31,6 +32,7 @@ function Api(files, options) {
this.stats = [];
this.tests = [];
this.files = files || [];
this.base = '';

Object.keys(Api.prototype).forEach(function (key) {
this[key] = this[key].bind(this);
Expand Down Expand Up @@ -106,17 +108,10 @@ Api.prototype._prefixTitle = function (file) {

var separator = ' ' + chalk.gray.dim(figures.pointerSmall) + ' ';

var base = path.dirname(this.files[0]);

if (base === '.') {
base = this.files[0] || 'test';
}

base += path.sep;

var prefix = path.relative('.', file)
.replace(base, '')
.replace(this.base, '')
.replace(/\.spec/, '')
.replace(/\.test/, '')
.replace(/test\-/g, '')
.replace(/\.js$/, '')
.split(path.sep)
Expand All @@ -143,6 +138,8 @@ Api.prototype.run = function () {

self.fileCount = files.length;

self.base = path.relative('.', commondir('.', files)) + path.sep;

var tests = files.map(self._runFile);

// receive test count from all files and then run the tests
Expand Down Expand Up @@ -195,7 +192,7 @@ function handlePaths(files) {
files = [
'test.js',
'test-*.js',
'test/*.js'
'test'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the change to this array is wrong.

It should match test.js and test-*.js in root, and any JS file recursively in test folder as long as it's not prefixed with an underscore.

];
}

Expand All @@ -209,7 +206,7 @@ function handlePaths(files) {
return files
.map(function (file) {
if (fs.statSync(file).isDirectory()) {
return handlePaths([path.join(file, '*.js')]);
return handlePaths([path.join(file, '**', '*.js')]);
}

return file;
Expand Down
3 changes: 2 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ var cli = meow([
' ava',
' ava test.js test2.js',
' ava test-*.js',
' ava test',
' ava --init',
' ava --init foo.js',
'',
'Default patterns when no arguments:',
'test.js test-*.js test/*.js'
'test.js test-*.js test/**/*.js'
], {
string: [
'_',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"cacha": "^1.0.3",
"chalk": "^1.0.0",
"co-with-promise": "^4.6.0",
"commondir": "^1.0.1",
"core-assert": "^0.1.0",
"debug": "^2.2.0",
"deeper": "^2.1.0",
Expand Down
5 changes: 3 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,15 @@ $ ava --help
ava
ava test.js test2.js
ava test-*.js
ava test
ava --init
ava --init foo.js

Default patterns when no arguments:
test.js test-*.js test/*.js
test.js test-*.js test/**/*.js
```

Files in directories named `fixtures` and `helpers` are ignored, as well as files starting with `_`. This can be useful for having helpers in the same directory as your test files.
Directories are recursive by default. Files in directories named `fixtures` and `helpers` are ignored, as well as files starting with `_`. This can be useful for having helpers in the same directory as your test files.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be explained better. Not clear what it means.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sindresorhus: Sorry, I'm really terrible with wording. Would something like "Directories will be expanded recursively" be better?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not totally happy with this, but something like:

By default, AVA run tests named test.js or starting with test- in root and any tests recursively in the test directory.

You also need to update the globbing pattern in the CLI help output (and the identical output in the readme).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sindresorhus: But that doesn't really indicate that any directories that you pass it, nor does it include the bit about fixtures and helpers or underscores.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ariporad Good point, should include that too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sindresorhus: Maybe something like

Directories will be recursively expanded. Files in directories named fixtures and helpers are ignored, as well as files starting with _. This can be useful for having helpers in the same directory as your test files.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


*WARNING: NON-STANDARD BEHAVIOR:* The AVA CLI will always try to find and use your projects local install of AVA. This is true even when you run the global `ava` command. This non-standard behavior solves an important [issue](https://github.com/sindresorhus/ava/issues/157), and should have no impact on everyday use.

Expand Down
45 changes: 39 additions & 6 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,21 @@ test('async/await support', function (t) {
});

test('test title prefixes', function (t) {
t.plan(5);
t.plan(6);

var separator = ' ' + figures.pointerSmall + ' ';
var files = [
path.join(__dirname, 'fixture/async-await.js'),
path.join(__dirname, 'fixture/es2015.js'),
path.join(__dirname, 'fixture/generators.js')
path.join(__dirname, 'fixture/generators.js'),
path.join(__dirname, 'fixture/subdir/in-a-subdir.js')
];
var expected = [
['async-await', 'async function'].join(separator),
['async-await', 'arrow async function'].join(separator),
['es2015', '[anonymous]'].join(separator),
['generators', 'generator function'].join(separator)
['generators', 'generator function'].join(separator),
['subdir', 'in-a-subdir', 'subdir'].join(separator)
];
var index;

Expand All @@ -64,8 +66,7 @@ test('test title prefixes', function (t) {
});

api.on('test', function (a) {
var unnecessaryString = 'test' + separator + 'fixture' + separator;
index = expected.indexOf(a.title.replace(unnecessaryString, ''));
index = expected.indexOf(a.title);

t.true(index >= 0);

Expand All @@ -88,7 +89,27 @@ test('display filename prefixes for failed test stack traces', function (t) {
.then(function () {
t.is(api.passCount, 2);
t.is(api.failCount, 1);
t.match(api.errors[0].title, /test \S fixture \S one-pass-one-fail \S this is a failing test/);
t.match(api.errors[0].title, /one-pass-one-fail \S this is a failing test/);
});
});

// This is a seperate test because we can't ensure the order of the errors (to match them), and this is easier than
// sorting.
test('display filename prefixes for failed test stack traces in subdirs', function (t) {
t.plan(3);

var files = [
path.join(__dirname, 'fixture/es2015.js'),
path.join(__dirname, 'fixture/subdir/failing-subdir.js')
];

var api = new Api(files);

api.run()
.then(function () {
t.is(api.passCount, 1);
t.is(api.failCount, 1);
t.match(api.errors[0].title, /subdir \S failing-subdir \S subdir fail/);
});
});

Expand Down Expand Up @@ -238,6 +259,18 @@ test('absolute paths', function (t) {
});
});

test('search directories recursivly for files', function (t) {
t.plan(1);

var api = new Api([path.join(__dirname, 'fixture/subdir')]);

api.run()
.then(function () {
t.is(api.passCount, 2);
t.is(api.failCount, 1);
});
});

test('titles of both passing and failing tests and AssertionErrors are returned', function (t) {
t.plan(3);

Expand Down
5 changes: 5 additions & 0 deletions test/fixture/subdir/failing-subdir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import test from '../../../';

test('subdir fail', t => {
t.fail();
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use ES2015 syntax like the other test fixtures.

5 changes: 5 additions & 0 deletions test/fixture/subdir/in-a-subdir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import test from '../../../';

test('subdir', t => {
t.pass();
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use ES2015 syntax like the other test fixtures.

5 changes: 5 additions & 0 deletions test/fixture/subdir/nested/nested.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import test from '../../../../';

test('subdir', t => {
t.pass();
});