From e3c44e35a0012330ac890d891b20ba9e7f6812d9 Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Tue, 28 Feb 2023 22:28:34 +0200 Subject: [PATCH] test_runner: unify duplicate code --- lib/internal/test_runner/harness.js | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/lib/internal/test_runner/harness.js b/lib/internal/test_runner/harness.js index de21ea4ad2326a..f6e99478ba54b5 100644 --- a/lib/internal/test_runner/harness.js +++ b/lib/internal/test_runner/harness.js @@ -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) { @@ -224,7 +217,7 @@ function hook(hook) { module.exports = { createTestTree, - test, + test: runInParentContext(Test, false), describe: runInParentContext(Suite), it: runInParentContext(ItTest), before: hook('before'),