UNPKG

2.26 kBJavaScriptView Raw
1'use strict';
2var path = require('path');
3var chalk = require('chalk');
4var serializeError = require('serialize-error');
5var beautifyStack = require('./lib/beautify-stack');
6var globals = require('./lib/globals');
7var Runner = require('./lib/runner');
8var send = require('./lib/send');
9
10// note that test files have require('ava')
11require('./lib/test-worker').avaRequired = true;
12
13var opts = globals.options;
14var runner = new Runner({
15 serial: opts.serial,
16 bail: opts.failFast,
17 match: opts.match
18});
19
20// check if the test is being run without AVA cli
21var isForked = typeof process.send === 'function';
22
23if (!isForked) {
24 var fp = path.relative('.', process.argv[1]);
25
26 console.log();
27 console.error('Test files must be run with the AVA CLI:\n\n ' + chalk.grey.dim('$') + ' ' + chalk.cyan('ava ' + fp) + '\n');
28
29 process.exit(1);
30}
31
32// if fail-fast is enabled, use this variable to detect
33// that no more tests should be logged
34var isFailed = false;
35
36Error.stackTraceLimit = Infinity;
37
38function test(props) {
39 if (isFailed) {
40 return;
41 }
42
43 var hasError = typeof props.error !== 'undefined';
44
45 // don't display anything if it's a passed hook
46 if (!hasError && props.type !== 'test') {
47 return;
48 }
49
50 if (hasError) {
51 props.error = serializeError(props.error);
52 if (props.error.stack) {
53 props.error.stack = beautifyStack(props.error.stack);
54 }
55 } else {
56 props.error = null;
57 }
58
59 send('test', props);
60
61 if (hasError && opts.failFast) {
62 isFailed = true;
63 exit();
64 }
65}
66
67function exit() {
68 send('results', {
69 stats: runner.stats
70 });
71}
72
73globals.setImmediate(function () {
74 var hasExclusive = runner.tests.hasExclusive;
75 var numberOfTests = runner.tests.tests.concurrent.length + runner.tests.tests.serial.length;
76
77 if (numberOfTests === 0) {
78 send('no-tests', {avaRequired: true});
79 return;
80 }
81
82 send('stats', {
83 testCount: numberOfTests,
84 hasExclusive: hasExclusive
85 });
86
87 runner.on('test', test);
88
89 process.on('ava-run', function (options) {
90 runner.run(options).then(exit);
91 });
92});
93
94module.exports = runner.test;
95// TypeScript imports the `default` property for
96// an ES2015 default import (`import test from 'ava'`)
97// See: https://github.com/Microsoft/TypeScript/issues/2242#issuecomment-83694181
98module.exports.default = runner.test;