diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index ee2c345e7a0403..0ef37aa31f7bc7 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -6,6 +6,7 @@ const { ArrayPrototypeShift, ArrayPrototypeSlice, ArrayPrototypeSome, + ArrayPrototypeForEach, ArrayPrototypeUnshift, FunctionPrototype, MathMax, @@ -122,6 +123,30 @@ class TestContext { constructor(test) { this.#test = test; + + this.test = (name, options, fn) => { + const overrides = { + __proto__: null, + loc: getCallerLocation(), + }; + // eslint-disable-next-line no-use-before-define + const subtest = this.#test.createSubtest(Test, name, options, fn, overrides); + return subtest.start(); + }; + + ArrayPrototypeForEach(['skip', 'todo', 'only'], (keyword) => { + this.test[keyword] = (name, options, fn) => { + const overrides = { + __proto__: null, + [keyword]: true, + loc: getCallerLocation(), + }; + + // eslint-disable-next-line no-use-before-define + const subtest = this.#test.createSubtest(Test, name, options, fn, overrides); + return subtest.start(); + }; + }); } get signal() { @@ -161,20 +186,6 @@ class TestContext { this.#test.todo(message); } - test(name, options, fn) { - const overrides = { - __proto__: null, - loc: getCallerLocation(), - }; - - const subtest = this.#test.createSubtest( - // eslint-disable-next-line no-use-before-define - Test, name, options, fn, overrides, - ); - - return subtest.start(); - } - before(fn, options) { this.#test.createHook('before', fn, options); } diff --git a/test/fixtures/test-runner/output/only_tests.js b/test/fixtures/test-runner/output/only_tests.js index 26266b524454b7..c091a26f477314 100644 --- a/test/fixtures/test-runner/output/only_tests.js +++ b/test/fixtures/test-runner/output/only_tests.js @@ -47,6 +47,12 @@ test('only = true, with subtests', { only: true }, async (t) => { await t.test('skipped subtest 4', { skip: true }); }); +test('only = true, with subtests and test.only', { only: true }, async (t) => { + await t.test('skipped subtest 1'); + await t.test.skip('skipped subtest 2'); + await t.test.only('running subtest 3'); +}); + describe.only('describe only = true, with subtests', () => { it.only('`it` subtest 1 should run', () => {}); diff --git a/test/fixtures/test-runner/output/only_tests.snapshot b/test/fixtures/test-runner/output/only_tests.snapshot index ded19f3bec4c6a..c9cca15a62a57a 100644 --- a/test/fixtures/test-runner/output/only_tests.snapshot +++ b/test/fixtures/test-runner/output/only_tests.snapshot @@ -116,6 +116,27 @@ ok 11 - only = true, with subtests --- duration_ms: * ... +# Subtest: only = true, with subtests and test.only + # Subtest: skipped subtest 1 + ok 1 - skipped subtest 1 + --- + duration_ms: * + ... + # Subtest: skipped subtest 2 + ok 2 - skipped subtest 2 # SKIP + --- + duration_ms: * + ... + # Subtest: running subtest 3 + ok 3 - running subtest 3 + --- + duration_ms: * + ... + 1..3 +ok 12 - only = true, with subtests and test.only + --- + duration_ms: * + ... # Subtest: describe only = true, with subtests # Subtest: `it` subtest 1 should run ok 1 - `it` subtest 1 should run @@ -128,7 +149,7 @@ ok 11 - only = true, with subtests duration_ms: * ... 1..2 -ok 12 - describe only = true, with subtests +ok 13 - describe only = true, with subtests --- duration_ms: * type: 'suite' @@ -195,7 +216,7 @@ ok 12 - describe only = true, with subtests duration_ms: * ... 1..12 -ok 13 - describe only = true, with a mixture of subtests +ok 14 - describe only = true, with a mixture of subtests --- duration_ms: * type: 'suite' @@ -217,17 +238,17 @@ ok 13 - describe only = true, with a mixture of subtests duration_ms: * ... 1..3 -ok 14 - describe only = true, with subtests +ok 15 - describe only = true, with subtests --- duration_ms: * type: 'suite' ... -1..14 -# tests 40 +1..15 +# tests 44 # suites 3 -# pass 15 +# pass 18 # fail 0 # cancelled 0 -# skipped 25 +# skipped 26 # todo 0 # duration_ms * diff --git a/test/fixtures/test-runner/output/sub_tests.js b/test/fixtures/test-runner/output/sub_tests.js new file mode 100644 index 00000000000000..e9bc669bcebe3e --- /dev/null +++ b/test/fixtures/test-runner/output/sub_tests.js @@ -0,0 +1,9 @@ +'use strict'; +require('../../../common'); +const { test } = require('node:test'); + +test('parent 1', async (t) => { + await t.test('running subtest 1'); + await t.test.skip('skipped subtest 2'); + await t.test.todo('mark subtest 3 as todo'); +}); diff --git a/test/fixtures/test-runner/output/sub_tests.snapshot b/test/fixtures/test-runner/output/sub_tests.snapshot new file mode 100644 index 00000000000000..a37b7964c6939e --- /dev/null +++ b/test/fixtures/test-runner/output/sub_tests.snapshot @@ -0,0 +1,31 @@ +TAP version 13 +# Subtest: parent 1 + # Subtest: running subtest 1 + ok 1 - running subtest 1 + --- + duration_ms: * + ... + # Subtest: skipped subtest 2 + ok 2 - skipped subtest 2 # SKIP + --- + duration_ms: * + ... + # Subtest: mark subtest 3 as todo + ok 3 - mark subtest 3 as todo # TODO + --- + duration_ms: * + ... + 1..3 +ok 1 - parent 1 + --- + duration_ms: * + ... +1..1 +# tests 4 +# suites 0 +# pass 2 +# fail 0 +# cancelled 0 +# skipped 1 +# todo 1 +# duration_ms * diff --git a/test/parallel/test-runner-output.mjs b/test/parallel/test-runner-output.mjs index e40ce896bff465..dcc57de79ef3e0 100644 --- a/test/parallel/test-runner-output.mjs +++ b/test/parallel/test-runner-output.mjs @@ -101,6 +101,7 @@ const tests = [ { name: 'test-runner/output/no_refs.js' }, { name: 'test-runner/output/no_tests.js' }, { name: 'test-runner/output/only_tests.js' }, + { name: 'test-runner/output/sub_tests.js' }, { name: 'test-runner/output/dot_reporter.js' }, { name: 'test-runner/output/junit_reporter.js', transform: junitTransform }, { name: 'test-runner/output/spec_reporter_successful.js', transform: specTransform },