Skip to content

Commit d9021c1

Browse files
committed
test_runner: unify duplicate code
1 parent 76bcf1a commit d9021c1

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

lib/internal/test_runner/harness.js

+10-17
Original file line numberDiff line numberDiff line change
@@ -186,33 +186,26 @@ async function startSubtest(subtest) {
186186
await subtest.start();
187187
}
188188

189-
function test(name, options, fn) {
190-
const parent = testResources.get(executionAsyncId()) || getGlobalRoot();
191-
const subtest = parent.createSubtest(Test, name, options, fn);
192-
if (!(parent instanceof Suite)) {
193-
return startSubtest(subtest);
194-
}
195-
}
196-
197-
function runInParentContext(Factory) {
189+
function runInParentContext(Factory, addShorthands = true) {
198190
function run(name, options, fn, overrides) {
199191
const parent = testResources.get(executionAsyncId()) || getGlobalRoot();
200192
const subtest = parent.createSubtest(Factory, name, options, fn, overrides);
201-
if (parent === getGlobalRoot()) {
202-
startSubtest(subtest);
193+
if (!(parent instanceof Suite)) {
194+
return startSubtest(subtest);
203195
}
204196
}
205197

206-
const cb = (name, options, fn) => {
207-
run(name, options, fn);
208-
};
198+
const test = (name, options, fn) => run(name, options, fn);
199+
if (!addShorthands) {
200+
return test;
201+
}
209202

210203
ArrayPrototypeForEach(['skip', 'todo', 'only'], (keyword) => {
211-
cb[keyword] = (name, options, fn) => {
204+
test[keyword] = (name, options, fn) => {
212205
run(name, options, fn, { [keyword]: true });
213206
};
214207
});
215-
return cb;
208+
return test;
216209
}
217210

218211
function hook(hook) {
@@ -224,7 +217,7 @@ function hook(hook) {
224217

225218
module.exports = {
226219
createTestTree,
227-
test,
220+
test: runInParentContext(Test, false),
228221
describe: runInParentContext(Suite),
229222
it: runInParentContext(ItTest),
230223
before: hook('before'),

0 commit comments

Comments
 (0)