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: nodejs#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>
  • Loading branch information
2 people authored and EliphazBouye committed Jun 20, 2024
1 parent f168357 commit 66ccac2
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 @@ -3047,6 +3047,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 66ccac2

Please sign in to comment.