Skip to content

Commit f6d2af8

Browse files
cjihrigJakobJingleheimer
authored andcommitted
test_runner: add context.fullName
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]>
1 parent 0b375a3 commit f6d2af8

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

doc/api/test.md

+8
Original file line numberDiff line numberDiff line change
@@ -3034,6 +3034,14 @@ test('top level test', (t) => {
30343034
});
30353035
```
30363036

3037+
### `context.fullName`
3038+
3039+
<!-- YAML
3040+
added: REPLACEME
3041+
-->
3042+
3043+
The name of the test and each of its ancestors, separated by `>`.
3044+
30373045
### `context.name`
30383046

30393047
<!-- YAML

lib/internal/test_runner/test.js

+18
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ class TestContext {
218218
return this.#test.name;
219219
}
220220

221+
get fullName() {
222+
return getFullName(this.#test);
223+
}
224+
221225
get error() {
222226
return this.#test.error;
223227
}
@@ -331,6 +335,10 @@ class SuiteContext {
331335
get name() {
332336
return this.#suite.name;
333337
}
338+
339+
get fullName() {
340+
return getFullName(this.#suite);
341+
}
334342
}
335343

336344
class Test extends AsyncResource {
@@ -1216,6 +1224,16 @@ class Suite extends Test {
12161224
}
12171225
}
12181226

1227+
function getFullName(test) {
1228+
let fullName = test.name;
1229+
1230+
for (let t = test.parent; t !== t.root; t = t.parent) {
1231+
fullName = `${t.name} > ${fullName}`;
1232+
}
1233+
1234+
return fullName;
1235+
}
1236+
12191237
module.exports = {
12201238
kCancelledByParent,
12211239
kSubtestsFailed,
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
require('../common');
3+
const { strictEqual } = require('node:assert');
4+
const { suite, test } = require('node:test');
5+
6+
suite('suite', (t) => {
7+
strictEqual(t.fullName, 'suite');
8+
9+
test('test', (t) => {
10+
strictEqual(t.fullName, 'suite > test');
11+
12+
t.test('subtest', (t) => {
13+
strictEqual(t.fullName, 'suite > test > subtest');
14+
15+
t.test('subsubtest', (t) => {
16+
strictEqual(t.fullName, 'suite > test > subtest > subsubtest');
17+
});
18+
});
19+
});
20+
});
21+
22+
test((t) => {
23+
strictEqual(t.fullName, '<anonymous>');
24+
});

0 commit comments

Comments
 (0)