UNPKG

2.03 kBJavaScriptView Raw
1'use strict';
2var loudRejection = require('loud-rejection/api')(process);
3var resolveFrom = require('resolve-from');
4var createEspowerPlugin = require('babel-plugin-espower/create');
5var requireFromString = require('require-from-string');
6var serializeValue = require('./serialize-value');
7
8var hasGenerators = parseInt(process.version.slice(1), 10) > 0;
9var testPath = process.argv[2];
10var babel;
11
12try {
13 var localBabel = resolveFrom('.', 'babel-core') || resolveFrom('.', 'babel');
14 babel = require(localBabel);
15} catch (err) {
16 babel = require('babel-core');
17}
18
19var options = {
20 blacklist: hasGenerators ? ['regenerator'] : [],
21 optional: hasGenerators ? ['asyncToGenerator', 'runtime'] : ['runtime'],
22 plugins: [
23 createEspowerPlugin(babel, {
24 patterns: require('./enhance-assert').PATTERNS
25 })
26 ]
27};
28
29var avaRequired;
30
31module.exports = {
32 avaRequired: function () {
33 avaRequired = true;
34 }
35};
36
37function send(name, data) {
38 process.send({name: name, data: data});
39}
40
41process.on('uncaughtException', function (exception) {
42 send('uncaughtException', {uncaughtException: serializeValue(exception)});
43});
44
45var transpiled = babel.transformFileSync(testPath, options);
46requireFromString(transpiled.code, testPath, {
47 appendPaths: module.paths
48});
49
50if (!avaRequired) {
51 throw new Error('No tests found in ' + testPath + ', make sure to import "ava" at the top of your test file');
52}
53
54process.on('message', function (message) {
55 var command = message['ava-child-process-command'];
56 if (command) {
57 process.emit('ava-' + command, message.data);
58 }
59});
60
61process.on('ava-kill', function () {
62 setTimeout(function () {
63 process.exit(0);
64 }, process.env.AVA_APPVEYOR ? 100 : 0);
65});
66
67process.on('ava-cleanup', function () {
68 var unhandled = loudRejection.currentlyUnhandled();
69 if (unhandled.length) {
70 unhandled = unhandled.map(function (entry) {
71 return serializeValue(entry.reason);
72 });
73 send('unhandledRejections', {unhandledRejections: unhandled});
74 }
75
76 setTimeout(function () {
77 send('cleaned-up', {});
78 }, 100);
79});