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: https://github.com/nodejs/node/pull/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>
This commit is contained in:
cjihrig 2024-05-26 12:36:46 -04:00
parent 063afa85fe
commit 90e81aaad7
No known key found for this signature in database
GPG Key ID: 7434390BDBE9B9C5
3 changed files with 50 additions and 0 deletions

View File

@ -3044,6 +3044,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

View File

@ -218,6 +218,10 @@ class TestContext {
return this.#test.name;
}
get fullName() {
return getFullName(this.#test);
}
get error() {
return this.#test.error;
}
@ -331,6 +335,10 @@ class SuiteContext {
get name() {
return this.#suite.name;
}
get fullName() {
return getFullName(this.#suite);
}
}
class Test extends AsyncResource {
@ -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,

View File

@ -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>');
});