diff --git a/api.js b/api.js index 0101e9366..1f4a5404f 100644 --- a/api.js +++ b/api.js @@ -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(); @@ -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); @@ -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) @@ -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 @@ -195,7 +192,7 @@ function handlePaths(files) { files = [ 'test.js', 'test-*.js', - 'test/*.js' + 'test' ]; } @@ -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; diff --git a/cli.js b/cli.js index 8c7adfc36..2301a04e6 100755 --- a/cli.js +++ b/cli.js @@ -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: [ '_', diff --git a/package.json b/package.json index 824043e55..92a83ff1a 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/readme.md b/readme.md index bebfdb0ba..7a16c0489 100644 --- a/readme.md +++ b/readme.md @@ -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. *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. diff --git a/test/api.js b/test/api.js index 53c3f7b94..ddcd689a1 100644 --- a/test/api.js +++ b/test/api.js @@ -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; @@ -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); @@ -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/); }); }); @@ -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); diff --git a/test/fixture/subdir/failing-subdir.js b/test/fixture/subdir/failing-subdir.js new file mode 100644 index 000000000..351c571e7 --- /dev/null +++ b/test/fixture/subdir/failing-subdir.js @@ -0,0 +1,5 @@ +import test from '../../../'; + +test('subdir fail', t => { + t.fail(); +}); diff --git a/test/fixture/subdir/in-a-subdir.js b/test/fixture/subdir/in-a-subdir.js new file mode 100644 index 000000000..3022a6714 --- /dev/null +++ b/test/fixture/subdir/in-a-subdir.js @@ -0,0 +1,5 @@ +import test from '../../../'; + +test('subdir', t => { + t.pass(); +}); diff --git a/test/fixture/subdir/nested/nested.js b/test/fixture/subdir/nested/nested.js new file mode 100644 index 000000000..88e68ff6b --- /dev/null +++ b/test/fixture/subdir/nested/nested.js @@ -0,0 +1,5 @@ +import test from '../../../../'; + +test('subdir', t => { + t.pass(); +});