Skip to content

Commit 9740986

Browse files
committed
Merge pull request #208 from jamestalmage/fix-198
Properly handle empty results from test files - fixes #198
2 parents fc9a1dc + a3308d2 commit 9740986

File tree

7 files changed

+68
-1
lines changed

7 files changed

+68
-1
lines changed

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
require('./lib/babel').avaRequired();
23
var setImmediate = require('set-immediate-shim');
34
var hasFlag = require('has-flag');
45
var chalk = require('chalk');

lib/babel.js

+15
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,26 @@ var options = {
2424
]
2525
};
2626

27+
var avaRequired;
28+
29+
module.exports = {
30+
avaRequired: function () {
31+
avaRequired = true;
32+
}
33+
};
34+
2735
var transpiled = babel.transformFileSync(testPath, options);
2836
requireFromString(transpiled.code, testPath, {
2937
appendPaths: module.paths
3038
});
3139

40+
if (!avaRequired) {
41+
console.error('No tests found in ' + testPath + ', make sure to import "ava" at the top of your test file');
42+
setImmediate(function () {
43+
process.exit(1);
44+
});
45+
}
46+
3247
process.on('message', function (message) {
3348
if (message['ava-kill-command']) {
3449
process.exit(0);

lib/fork.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,19 @@ module.exports = function (args) {
3434
ps.on('exit', function (code) {
3535
if (code > 0 && code !== 143) {
3636
reject(new Error(file + ' exited with a non-zero exit code: ' + code));
37-
} else {
37+
} else if (testResults) {
38+
if (!testResults.tests.length) {
39+
testResults.stats.failCount++;
40+
testResults.tests.push({
41+
duration: 0,
42+
title: file,
43+
error: new Error('No tests for ' + file),
44+
type: 'test'
45+
});
46+
}
3847
resolve(testResults);
48+
} else {
49+
reject(new Error('Never got test results from: ' + file));
3950
}
4051
});
4152
});

test/fixture/empty.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
__
3+
____ _____ _______/ |_ ___.__.
4+
_/ __ \ / \\____ \ __< | |
5+
\ ___/| Y Y \ |_> > | \___ |
6+
\___ >__|_| / __/|__| / ____|
7+
\/ \/|__| \/
8+
*/

test/fixture/immediate-0-exit.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
process.exit(0);

test/fixture/no-tests.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import test from '../../';

test/test.js

+30
Original file line numberDiff line numberDiff line change
@@ -1087,3 +1087,33 @@ test('titles of both passing and failing tests and AssertionErrors are displayed
10871087
t.end();
10881088
});
10891089
});
1090+
1091+
test('empty test files creates a failure with a helpful warning', function (t) {
1092+
t.plan(2);
1093+
1094+
execCli('fixture/empty.js', function (err, stdout) {
1095+
t.ok(err);
1096+
t.ok(/No tests found.*?import "ava"/.test(stdout));
1097+
t.end();
1098+
});
1099+
});
1100+
1101+
test('test file with no tests creates a failure with a helpful warning', function (t) {
1102+
t.plan(2);
1103+
1104+
execCli('fixture/no-tests.js', function (err, stdout, stderr) {
1105+
t.ok(err);
1106+
t.ok(/No tests/.test(stderr));
1107+
t.end();
1108+
});
1109+
});
1110+
1111+
test('test file that immediately exits with 0 exit code ', function (t) {
1112+
t.plan(2);
1113+
1114+
execCli('fixture/immediate-0-exit.js', function (err, stdout, stderr) {
1115+
t.ok(err);
1116+
t.ok(/Never got test results/.test(stderr));
1117+
t.end();
1118+
});
1119+
});

0 commit comments

Comments
 (0)