Skip to content

Commit

Permalink
test_runner: add context.fullName
Browse files Browse the repository at this point in the history
This commit adds a fullName getter to the TestContext and
SuiteContext classes. This is similar to the existing name getter,
but also includes the name of all ancestor tests/suites.

PR-URL: #53169
Reviewed-By: Moshe Atlow <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Geoffrey Booth <[email protected]>
Co-authored-by: Jacob Smith <[email protected]>
  • Loading branch information
2 people authored and targos committed Jun 1, 2024
1 parent 0b375a3 commit f6d2af8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
8 changes: 8 additions & 0 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -3034,6 +3034,14 @@ test('top level test', (t) => {
});
```

### `context.fullName`

<!-- YAML
added: REPLACEME
-->

The name of the test and each of its ancestors, separated by `>`.

### `context.name`

<!-- YAML
Expand Down
18 changes: 18 additions & 0 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ class TestContext {
return this.#test.name;
}

get fullName() {
return getFullName(this.#test);
}

get error() {
return this.#test.error;
}
Expand Down Expand Up @@ -331,6 +335,10 @@ class SuiteContext {
get name() {
return this.#suite.name;
}

get fullName() {
return getFullName(this.#suite);
}
}

class Test extends AsyncResource {
Expand Down Expand Up @@ -1216,6 +1224,16 @@ class Suite extends Test {
}
}

function getFullName(test) {
let fullName = test.name;

for (let t = test.parent; t !== t.root; t = t.parent) {
fullName = `${t.name} > ${fullName}`;
}

return fullName;
}

module.exports = {
kCancelledByParent,
kSubtestsFailed,
Expand Down
24 changes: 24 additions & 0 deletions test/parallel/test-runner-test-fullname.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';
require('../common');
const { strictEqual } = require('node:assert');
const { suite, test } = require('node:test');

suite('suite', (t) => {
strictEqual(t.fullName, 'suite');

test('test', (t) => {
strictEqual(t.fullName, 'suite > test');

t.test('subtest', (t) => {
strictEqual(t.fullName, 'suite > test > subtest');

t.test('subsubtest', (t) => {
strictEqual(t.fullName, 'suite > test > subtest > subsubtest');
});
});
});
});

test((t) => {
strictEqual(t.fullName, '<anonymous>');
});

0 comments on commit f6d2af8

Please sign in to comment.