Skip to content

Commit

Permalink
test_runner: fix #50665 .skip, .todo and .only missing in subtests
Browse files Browse the repository at this point in the history
  • Loading branch information
rluvaton committed Jan 20, 2024
1 parent 27d839f commit 8ba986d
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 21 deletions.
39 changes: 25 additions & 14 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
ArrayPrototypeShift,
ArrayPrototypeSlice,
ArrayPrototypeSome,
ArrayPrototypeForEach,
ArrayPrototypeUnshift,
FunctionPrototype,
MathMax,
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/test-runner/output/only_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {});

Expand Down
35 changes: 28 additions & 7 deletions test/fixtures/test-runner/output/only_tests.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
Expand Down Expand Up @@ -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'
Expand All @@ -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 *
9 changes: 9 additions & 0 deletions test/fixtures/test-runner/output/sub_tests.js
Original file line number Diff line number Diff line change
@@ -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');
});
31 changes: 31 additions & 0 deletions test/fixtures/test-runner/output/sub_tests.snapshot
Original file line number Diff line number Diff line change
@@ -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 *
1 change: 1 addition & 0 deletions test/parallel/test-runner-output.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down

0 comments on commit 8ba986d

Please sign in to comment.