-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure test files load correct AVA installation (#1085)
The main process sets the AVA_PATH environment variable to the absolute path of the index.js file. Workers are loaded with this variable present. When test files require the AVA module (assuming it's a version containing this commit of course), the index.js file redirects to the one used by the worker if necessary by comparing AVA_PATH. The redirect required most of index.js to be moved into a separate module (lib/main.js). Fixes #643.
- Loading branch information
1 parent
476c653
commit 3ea2ba1
Showing
6 changed files
with
123 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,8 @@ | ||
'use strict'; | ||
var process = require('./lib/process-adapter'); | ||
var serializeError = require('./lib/serialize-error'); | ||
var globals = require('./lib/globals'); | ||
var Runner = require('./lib/runner'); | ||
var send = process.send; | ||
|
||
var opts = globals.options; | ||
var runner = new Runner({ | ||
serial: opts.serial, | ||
bail: opts.failFast, | ||
match: opts.match | ||
}); | ||
|
||
// note that test files have require('ava') | ||
require('./lib/test-worker').avaRequired = true; | ||
|
||
// if fail-fast is enabled, use this variable to detect | ||
// that no more tests should be logged | ||
var isFailed = false; | ||
|
||
Error.stackTraceLimit = Infinity; | ||
|
||
function test(props) { | ||
if (isFailed) { | ||
return; | ||
} | ||
|
||
var hasError = typeof props.error !== 'undefined'; | ||
|
||
// don't display anything if it's a passed hook | ||
if (!hasError && props.type !== 'test') { | ||
return; | ||
} | ||
|
||
if (hasError) { | ||
props.error = serializeError(props.error); | ||
} else { | ||
props.error = null; | ||
} | ||
|
||
send('test', props); | ||
|
||
if (hasError && opts.failFast) { | ||
isFailed = true; | ||
exit(); | ||
} | ||
} | ||
|
||
function exit() { | ||
var stats = runner._buildStats(); | ||
|
||
send('results', { | ||
stats: stats | ||
}); | ||
// Ensure the same AVA install is loaded by the test file as by the test worker. | ||
if (process.env.AVA_PATH && process.env.AVA_PATH !== __dirname) { | ||
module.exports = require(process.env.AVA_PATH); // eslint-disable-line import/no-dynamic-require | ||
} else { | ||
module.exports = require('./lib/main'); | ||
} | ||
|
||
globals.setImmediate(function () { | ||
var hasExclusive = runner.tests.hasExclusive; | ||
var numberOfTests = runner.tests.tests.concurrent.length + runner.tests.tests.serial.length; | ||
|
||
if (numberOfTests === 0) { | ||
send('no-tests', {avaRequired: true}); | ||
return; | ||
} | ||
|
||
send('stats', { | ||
testCount: numberOfTests, | ||
hasExclusive: hasExclusive | ||
}); | ||
|
||
runner.on('test', test); | ||
|
||
process.on('ava-run', function (options) { | ||
runner.run(options).then(exit); | ||
}); | ||
|
||
process.on('ava-init-exit', function () { | ||
exit(); | ||
}); | ||
}); | ||
|
||
module.exports = runner.test; | ||
|
||
// TypeScript imports the `default` property for | ||
// an ES2015 default import (`import test from 'ava'`) | ||
// See: https://github.com/Microsoft/TypeScript/issues/2242#issuecomment-83694181 | ||
module.exports.default = runner.test; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
'use strict'; | ||
|
||
var process = require('./process-adapter'); | ||
var serializeError = require('./serialize-error'); | ||
var globals = require('./globals'); | ||
var Runner = require('./runner'); | ||
var send = process.send; | ||
|
||
var opts = globals.options; | ||
var runner = new Runner({ | ||
serial: opts.serial, | ||
bail: opts.failFast, | ||
match: opts.match | ||
}); | ||
|
||
// note that test files have require('ava') | ||
require('./test-worker').avaRequired = true; | ||
|
||
// if fail-fast is enabled, use this variable to detect | ||
// that no more tests should be logged | ||
var isFailed = false; | ||
|
||
Error.stackTraceLimit = Infinity; | ||
|
||
function test(props) { | ||
if (isFailed) { | ||
return; | ||
} | ||
|
||
var hasError = typeof props.error !== 'undefined'; | ||
|
||
// don't display anything if it's a passed hook | ||
if (!hasError && props.type !== 'test') { | ||
return; | ||
} | ||
|
||
if (hasError) { | ||
props.error = serializeError(props.error); | ||
} else { | ||
props.error = null; | ||
} | ||
|
||
send('test', props); | ||
|
||
if (hasError && opts.failFast) { | ||
isFailed = true; | ||
exit(); | ||
} | ||
} | ||
|
||
function exit() { | ||
var stats = runner._buildStats(); | ||
|
||
send('results', { | ||
stats: stats | ||
}); | ||
} | ||
|
||
globals.setImmediate(function () { | ||
var hasExclusive = runner.tests.hasExclusive; | ||
var numberOfTests = runner.tests.tests.concurrent.length + runner.tests.tests.serial.length; | ||
|
||
if (numberOfTests === 0) { | ||
send('no-tests', {avaRequired: true}); | ||
return; | ||
} | ||
|
||
send('stats', { | ||
testCount: numberOfTests, | ||
hasExclusive: hasExclusive | ||
}); | ||
|
||
runner.on('test', test); | ||
|
||
process.on('ava-run', function (options) { | ||
runner.run(options).then(exit); | ||
}); | ||
|
||
process.on('ava-init-exit', function () { | ||
exit(); | ||
}); | ||
}); | ||
|
||
module.exports = runner.test; | ||
|
||
// TypeScript imports the `default` property for | ||
// an ES2015 default import (`import test from 'ava'`) | ||
// See: https://github.com/Microsoft/TypeScript/issues/2242#issuecomment-83694181 | ||
module.exports.default = runner.test; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
/* eslint-disable import/no-unresolved */ | ||
import test from 'ava'; | ||
|
||
test(t => t.pass()); |