Skip to content

Commit ee2cdef

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: nodejs#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 a0408b2 commit ee2cdef

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

doc/api/test.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3044,6 +3044,14 @@ test('top level test', (t) => {
30443044
});
30453045
```
30463046

3047+
### `context.fullName`
3048+
3049+
<!-- YAML
3050+
added: REPLACEME
3051+
-->
3052+
3053+
The name of the test and each of its ancestors, separated by `>`.
3054+
30473055
### `context.name`
30483056

30493057
<!-- YAML

lib/internal/test_runner/test.js

Lines changed: 18 additions & 0 deletions
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,
Lines changed: 24 additions & 0 deletions
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)