mirror of
https://github.com/nodejs/node.git
synced 2025-04-30 15:41:06 +00:00

Some part of the codebase already use trailing commas, this commit is adding a lint rule to ensure it stays this way. This commit also adds the rule for a few files that were missing only one or two trailing commas. PR-URL: https://github.com/nodejs/node/pull/46655 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
212 lines
5.4 KiB
JavaScript
212 lines
5.4 KiB
JavaScript
'use strict';
|
|
const {
|
|
ArrayPrototypeForEach,
|
|
SafeMap,
|
|
SafeWeakSet,
|
|
} = primordials;
|
|
const {
|
|
createHook,
|
|
executionAsyncId,
|
|
} = require('async_hooks');
|
|
const {
|
|
codes: {
|
|
ERR_TEST_FAILURE,
|
|
},
|
|
} = require('internal/errors');
|
|
const { exitCodes: { kGenericUserError } } = internalBinding('errors');
|
|
|
|
const { kEmptyObject } = require('internal/util');
|
|
const { getOptionValue } = require('internal/options');
|
|
const { kCancelledByParent, Test, ItTest, Suite } = require('internal/test_runner/test');
|
|
const { setupTestReporters } = require('internal/test_runner/utils');
|
|
const { bigint: hrtime } = process.hrtime;
|
|
|
|
const isTestRunnerCli = getOptionValue('--test');
|
|
const testResources = new SafeMap();
|
|
const wasRootSetup = new SafeWeakSet();
|
|
|
|
function createTestTree(options = kEmptyObject) {
|
|
return setup(new Test({ __proto__: null, ...options, name: '<root>' }));
|
|
}
|
|
|
|
function createProcessEventHandler(eventName, rootTest) {
|
|
return (err) => {
|
|
// Check if this error is coming from a test. If it is, fail the test.
|
|
const test = testResources.get(executionAsyncId());
|
|
|
|
if (!test) {
|
|
throw err;
|
|
}
|
|
|
|
if (test.finished) {
|
|
// If the test is already finished, report this as a top level
|
|
// diagnostic since this is a malformed test.
|
|
const msg = `Warning: Test "${test.name}" generated asynchronous ` +
|
|
'activity after the test ended. This activity created the error ' +
|
|
`"${err}" and would have caused the test to fail, but instead ` +
|
|
`triggered an ${eventName} event.`;
|
|
|
|
rootTest.diagnostic(msg);
|
|
process.exitCode = kGenericUserError;
|
|
return;
|
|
}
|
|
|
|
test.fail(new ERR_TEST_FAILURE(err, eventName));
|
|
test.postRun();
|
|
};
|
|
}
|
|
|
|
function configureCoverage(rootTest) {
|
|
if (!getOptionValue('--experimental-test-coverage')) {
|
|
return null;
|
|
}
|
|
|
|
const { setupCoverage } = require('internal/test_runner/coverage');
|
|
|
|
try {
|
|
return setupCoverage();
|
|
} catch (err) {
|
|
const msg = `Warning: Code coverage could not be enabled. ${err}`;
|
|
|
|
rootTest.diagnostic(msg);
|
|
process.exitCode = kGenericUserError;
|
|
}
|
|
}
|
|
|
|
function collectCoverage(rootTest, coverage) {
|
|
if (!coverage) {
|
|
return null;
|
|
}
|
|
|
|
let summary = null;
|
|
|
|
try {
|
|
summary = coverage.summary();
|
|
coverage.cleanup();
|
|
} catch (err) {
|
|
const op = summary ? 'clean up' : 'report';
|
|
const msg = `Warning: Could not ${op} code coverage. ${err}`;
|
|
|
|
rootTest.diagnostic(msg);
|
|
process.exitCode = kGenericUserError;
|
|
}
|
|
|
|
return summary;
|
|
}
|
|
|
|
function setup(root) {
|
|
if (wasRootSetup.has(root)) {
|
|
return root;
|
|
}
|
|
const hook = createHook({
|
|
init(asyncId, type, triggerAsyncId, resource) {
|
|
if (resource instanceof Test) {
|
|
testResources.set(asyncId, resource);
|
|
return;
|
|
}
|
|
|
|
const parent = testResources.get(triggerAsyncId);
|
|
|
|
if (parent !== undefined) {
|
|
testResources.set(asyncId, parent);
|
|
}
|
|
},
|
|
destroy(asyncId) {
|
|
testResources.delete(asyncId);
|
|
},
|
|
});
|
|
|
|
hook.enable();
|
|
|
|
const exceptionHandler =
|
|
createProcessEventHandler('uncaughtException', root);
|
|
const rejectionHandler =
|
|
createProcessEventHandler('unhandledRejection', root);
|
|
const coverage = configureCoverage(root);
|
|
const exitHandler = () => {
|
|
root.coverage = collectCoverage(root, coverage);
|
|
root.postRun(new ERR_TEST_FAILURE(
|
|
'Promise resolution is still pending but the event loop has already resolved',
|
|
kCancelledByParent));
|
|
|
|
hook.disable();
|
|
process.removeListener('unhandledRejection', rejectionHandler);
|
|
process.removeListener('uncaughtException', exceptionHandler);
|
|
};
|
|
|
|
const terminationHandler = () => {
|
|
exitHandler();
|
|
process.exit();
|
|
};
|
|
|
|
process.on('uncaughtException', exceptionHandler);
|
|
process.on('unhandledRejection', rejectionHandler);
|
|
process.on('beforeExit', exitHandler);
|
|
// TODO(MoLow): Make it configurable to hook when isTestRunnerCli === false.
|
|
if (isTestRunnerCli) {
|
|
process.on('SIGINT', terminationHandler);
|
|
process.on('SIGTERM', terminationHandler);
|
|
}
|
|
|
|
root.startTime = hrtime();
|
|
|
|
wasRootSetup.add(root);
|
|
return root;
|
|
}
|
|
|
|
let globalRoot;
|
|
function getGlobalRoot() {
|
|
if (!globalRoot) {
|
|
globalRoot = createTestTree();
|
|
globalRoot.reporter.once('test:fail', () => {
|
|
process.exitCode = kGenericUserError;
|
|
});
|
|
setupTestReporters(globalRoot.reporter);
|
|
}
|
|
return globalRoot;
|
|
}
|
|
|
|
function test(name, options, fn) {
|
|
const subtest = getGlobalRoot().createSubtest(Test, name, options, fn);
|
|
return subtest.start();
|
|
}
|
|
|
|
function runInParentContext(Factory) {
|
|
function run(name, options, fn, overrides) {
|
|
const parent = testResources.get(executionAsyncId()) || getGlobalRoot();
|
|
const subtest = parent.createSubtest(Factory, name, options, fn, overrides);
|
|
if (parent === getGlobalRoot()) {
|
|
subtest.start();
|
|
}
|
|
}
|
|
|
|
const cb = (name, options, fn) => {
|
|
run(name, options, fn);
|
|
};
|
|
|
|
ArrayPrototypeForEach(['skip', 'todo'], (keyword) => {
|
|
cb[keyword] = (name, options, fn) => {
|
|
run(name, options, fn, { [keyword]: true });
|
|
};
|
|
});
|
|
return cb;
|
|
}
|
|
|
|
function hook(hook) {
|
|
return (fn, options) => {
|
|
const parent = testResources.get(executionAsyncId()) || getGlobalRoot();
|
|
parent.createHook(hook, fn, options);
|
|
};
|
|
}
|
|
|
|
module.exports = {
|
|
createTestTree,
|
|
test,
|
|
describe: runInParentContext(Suite),
|
|
it: runInParentContext(ItTest),
|
|
before: hook('before'),
|
|
after: hook('after'),
|
|
beforeEach: hook('beforeEach'),
|
|
afterEach: hook('afterEach'),
|
|
};
|