Skip to content

Commit

Permalink
test_runner: unify duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
MoLow committed Feb 28, 2023
1 parent 76bcf1a commit d9021c1
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,33 +186,26 @@ async function startSubtest(subtest) {
await subtest.start();
}

function test(name, options, fn) {
const parent = testResources.get(executionAsyncId()) || getGlobalRoot();
const subtest = parent.createSubtest(Test, name, options, fn);
if (!(parent instanceof Suite)) {
return startSubtest(subtest);
}
}

function runInParentContext(Factory) {
function runInParentContext(Factory, addShorthands = true) {
function run(name, options, fn, overrides) {
const parent = testResources.get(executionAsyncId()) || getGlobalRoot();
const subtest = parent.createSubtest(Factory, name, options, fn, overrides);
if (parent === getGlobalRoot()) {
startSubtest(subtest);
if (!(parent instanceof Suite)) {
return startSubtest(subtest);
}
}

const cb = (name, options, fn) => {
run(name, options, fn);
};
const test = (name, options, fn) => run(name, options, fn);
if (!addShorthands) {
return test;
}

ArrayPrototypeForEach(['skip', 'todo', 'only'], (keyword) => {
cb[keyword] = (name, options, fn) => {
test[keyword] = (name, options, fn) => {
run(name, options, fn, { [keyword]: true });
};
});
return cb;
return test;
}

function hook(hook) {
Expand All @@ -224,7 +217,7 @@ function hook(hook) {

module.exports = {
createTestTree,
test,
test: runInParentContext(Test, false),
describe: runInParentContext(Suite),
it: runInParentContext(ItTest),
before: hook('before'),
Expand Down

0 comments on commit d9021c1

Please sign in to comment.