diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-05-28 00:38:50 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-05-28 00:40:43 +0200 |
commit | 7fff4499fd915bcea3fa93b1aa8b35f4fe7a6027 (patch) | |
tree | 6de9a1aebd150a23b7f8c273ec657a5d0a18fe3e /node_modules/ava/lib/test-worker.js | |
parent | 963b7a41feb29cc4be090a2446bdfe0c1f1bcd81 (diff) |
add linting (and some initial fixes)
Diffstat (limited to 'node_modules/ava/lib/test-worker.js')
-rw-r--r-- | node_modules/ava/lib/test-worker.js | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/node_modules/ava/lib/test-worker.js b/node_modules/ava/lib/test-worker.js new file mode 100644 index 000000000..2df7f745d --- /dev/null +++ b/node_modules/ava/lib/test-worker.js @@ -0,0 +1,130 @@ +'use strict'; + +// Check if the test is being run without AVA cli +{ + /* eslint-disable import/order */ + const path = require('path'); + const chalk = require('chalk'); + + const isForked = typeof process.send === 'function'; + if (!isForked) { + const fp = path.relative('.', process.argv[1]); + + console.log(); + console.error('Test files must be run with the AVA CLI:\n\n ' + chalk.grey.dim('$') + ' ' + chalk.cyan('ava ' + fp) + '\n'); + + process.exit(1); // eslint-disable-line unicorn/no-process-exit + } +} + +/* eslint-enable import/order */ +const Bluebird = require('bluebird'); +const currentlyUnhandled = require('currently-unhandled')(); +const isObj = require('is-obj'); +const adapter = require('./process-adapter'); +const globals = require('./globals'); +const serializeError = require('./serialize-error'); + +const opts = adapter.opts; +const testPath = opts.file; +globals.options = opts; + +// Bluebird specific +Bluebird.longStackTraces(); + +(opts.require || []).forEach(require); + +adapter.installSourceMapSupport(); +adapter.installPrecompilerHook(); + +const dependencies = []; +adapter.installDependencyTracking(dependencies, testPath); + +// Set when main.js is required (since test files should have `require('ava')`). +let runner = null; +exports.setRunner = newRunner => { + runner = newRunner; +}; + +require(testPath); // eslint-disable-line import/no-dynamic-require + +// If AVA was not required, show an error +if (!runner) { + adapter.send('no-tests', {avaRequired: false}); +} + +function attributeLeakedError(err) { + if (!runner) { + return false; + } + + return runner.attributeLeakedError(err); +} + +const attributedRejections = new Set(); +process.on('unhandledRejection', (reason, promise) => { + if (attributeLeakedError(reason)) { + attributedRejections.add(promise); + } +}); + +process.on('uncaughtException', exception => { + if (attributeLeakedError(exception)) { + return; + } + + let serialized; + try { + serialized = serializeError(exception); + } catch (ignore) { // eslint-disable-line unicorn/catch-error-name + // Avoid using serializeError + const err = new Error('Failed to serialize uncaught exception'); + serialized = { + avaAssertionError: false, + name: err.name, + message: err.message, + stack: err.stack + }; + } + + // Ensure the IPC channel is refereced. The uncaught exception will kick off + // the teardown sequence, for which the messages must be received. + adapter.ipcChannel.ref(); + + adapter.send('uncaughtException', {exception: serialized}); +}); + +let tearingDown = false; +process.on('ava-teardown', () => { + // AVA-teardown can be sent more than once + if (tearingDown) { + return; + } + tearingDown = true; + + let rejections = currentlyUnhandled() + .filter(rejection => !attributedRejections.has(rejection.promise)); + + if (rejections.length > 0) { + rejections = rejections.map(rejection => { + let reason = rejection.reason; + if (!isObj(reason) || typeof reason.message !== 'string') { + reason = { + message: String(reason) + }; + } + return serializeError(reason); + }); + + adapter.send('unhandledRejections', {rejections}); + } + + // Include dependencies in the final teardown message. This ensures the full + // set of dependencies is included no matter how the process exits, unless + // it flat out crashes. + adapter.send('teardown', {dependencies}); +}); + +process.on('ava-exit', () => { + process.exit(0); // eslint-disable-line xo/no-process-exit +}); |