Skip to content

Commit a03a4c7

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 <moshe@atlow.co.il> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Co-authored-by: Jacob Smith <jacob@frende.me>
1 parent 0c8fcce commit a03a4c7

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
@@ -2900,6 +2900,14 @@ test('top level test', (t) => {
29002900
});
29012901
```
29022902

2903+
### `context.fullName`
2904+
2905+
<!-- YAML
2906+
added: REPLACEME
2907+
-->
2908+
2909+
The name of the test and each of its ancestors, separated by `>`.
2910+
29032911
### `context.name`
29042912

29052913
<!-- YAML

lib/internal/test_runner/test.js

+18
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ class TestContext {
201201
return this.#test.name;
202202
}
203203

204+
get fullName() {
205+
return getFullName(this.#test);
206+
}
207+
204208
get error() {
205209
return this.#test.error;
206210
}
@@ -314,6 +318,10 @@ class SuiteContext {
314318
get name() {
315319
return this.#suite.name;
316320
}
321+
322+
get fullName() {
323+
return getFullName(this.#suite);
324+
}
317325
}
318326

319327
class Test extends AsyncResource {
@@ -1139,6 +1147,16 @@ class Suite extends Test {
11391147
}
11401148
}
11411149

1150+
function getFullName(test) {
1151+
let fullName = test.name;
1152+
1153+
for (let t = test.parent; t !== t.root; t = t.parent) {
1154+
fullName = `${t.name} > ${fullName}`;
1155+
}
1156+
1157+
return fullName;
1158+
}
1159+
11421160
module.exports = {
11431161
kCancelledByParent,
11441162
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)