Skip to content

Commit

Permalink
Merge pull request #208 from jamestalmage/fix-198
Browse files Browse the repository at this point in the history
Properly handle empty results from test files - fixes #198
  • Loading branch information
sindresorhus committed Nov 14, 2015
2 parents fc9a1dc + a3308d2 commit 9740986
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
require('./lib/babel').avaRequired();
var setImmediate = require('set-immediate-shim');
var hasFlag = require('has-flag');
var chalk = require('chalk');
Expand Down
15 changes: 15 additions & 0 deletions lib/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,26 @@ var options = {
]
};

var avaRequired;

module.exports = {
avaRequired: function () {
avaRequired = true;
}
};

var transpiled = babel.transformFileSync(testPath, options);
requireFromString(transpiled.code, testPath, {
appendPaths: module.paths
});

if (!avaRequired) {
console.error('No tests found in ' + testPath + ', make sure to import "ava" at the top of your test file');
setImmediate(function () {
process.exit(1);
});
}

process.on('message', function (message) {
if (message['ava-kill-command']) {
process.exit(0);
Expand Down
13 changes: 12 additions & 1 deletion lib/fork.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,19 @@ module.exports = function (args) {
ps.on('exit', function (code) {
if (code > 0 && code !== 143) {
reject(new Error(file + ' exited with a non-zero exit code: ' + code));
} else {
} else if (testResults) {
if (!testResults.tests.length) {
testResults.stats.failCount++;
testResults.tests.push({
duration: 0,
title: file,
error: new Error('No tests for ' + file),
type: 'test'
});
}
resolve(testResults);
} else {
reject(new Error('Never got test results from: ' + file));
}
});
});
Expand Down
8 changes: 8 additions & 0 deletions test/fixture/empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
__
____ _____ _______/ |_ ___.__.
_/ __ \ / \\____ \ __< | |
\ ___/| Y Y \ |_> > | \___ |
\___ >__|_| / __/|__| / ____|
\/ \/|__| \/
*/
1 change: 1 addition & 0 deletions test/fixture/immediate-0-exit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
process.exit(0);
1 change: 1 addition & 0 deletions test/fixture/no-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import test from '../../';
30 changes: 30 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1087,3 +1087,33 @@ test('titles of both passing and failing tests and AssertionErrors are displayed
t.end();
});
});

test('empty test files creates a failure with a helpful warning', function (t) {
t.plan(2);

execCli('fixture/empty.js', function (err, stdout) {
t.ok(err);
t.ok(/No tests found.*?import "ava"/.test(stdout));
t.end();
});
});

test('test file with no tests creates a failure with a helpful warning', function (t) {
t.plan(2);

execCli('fixture/no-tests.js', function (err, stdout, stderr) {
t.ok(err);
t.ok(/No tests/.test(stderr));
t.end();
});
});

test('test file that immediately exits with 0 exit code ', function (t) {
t.plan(2);

execCli('fixture/immediate-0-exit.js', function (err, stdout, stderr) {
t.ok(err);
t.ok(/Never got test results/.test(stderr));
t.end();
});
});

0 comments on commit 9740986

Please sign in to comment.