GeezerVison is showing github.com — test_runner: add context.filePath · nodejs/node@698e44f
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/test.md +20Lines changed: 20 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -3194,6 +3194,16 @@ test('top level test', (t) => { | 3194 | 3194 | }); | 3195 | 3195 | ``` | 3196 | 3196 |
| | 3197 | +### `context.filePath` | | 3198 | + | | 3199 | +<!-- YAML | | 3200 | +added: REPLACEME | | 3201 | +--> | | 3202 | + | | 3203 | +The absolute path of the test file that created the current test. If a test file | | 3204 | +imports additional modules that generate tests, the imported tests will return | | 3205 | +the path of the root test file. | | 3206 | + | 3197 | 3207 | ### `context.fullName` | 3198 | 3208 |
| 3199 | 3209 | <!-- YAML | @@ -3427,6 +3437,16 @@ An instance of `SuiteContext` is passed to each suite function in order to | 3427 | 3437 | interact with the test runner. However, the `SuiteContext` constructor is not | 3428 | 3438 | exposed as part of the API. | 3429 | 3439 |
| | 3440 | +### `context.filePath` | | 3441 | + | | 3442 | +<!-- YAML | | 3443 | +added: REPLACEME | | 3444 | +--> | | 3445 | + | | 3446 | +The absolute path of the test file that created the current suite. If a test | | 3447 | +file imports additional modules that generate suites, the imported suites will | | 3448 | +return the path of the root test file. | | 3449 | + | 3430 | 3450 | ### `context.name` | 3431 | 3451 |
| 3432 | 3452 | <!-- YAML | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/test_runner/harness.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -221,7 +221,7 @@ let globalRoot; | 221 | 221 | let reportersSetup; | 222 | 222 | function getGlobalRoot() { | 223 | 223 | if (!globalRoot) { | 224 | | - globalRoot = createTestTree(); | | 224 | + globalRoot = createTestTree({ __proto__: null, entryFile: process.argv?.[1] }); | 225 | 225 | globalRoot.reporter.on('test:fail', (data) => { | 226 | 226 | if (data.todo === undefined || data.todo === false) { | 227 | 227 | process.exitCode = kGenericUserError; | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/test_runner/test.js +11-1Lines changed: 11 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -225,6 +225,10 @@ class TestContext { | 225 | 225 | return this.#test.name; | 226 | 226 | } | 227 | 227 |
| | 228 | + get filePath() { | | 229 | + return this.#test.entryFile; | | 230 | + } | | 231 | + | 228 | 232 | get fullName() { | 229 | 233 | return getFullName(this.#test); | 230 | 234 | } | @@ -343,6 +347,10 @@ class SuiteContext { | 343 | 347 | return this.#suite.name; | 344 | 348 | } | 345 | 349 |
| | 350 | + get filePath() { | | 351 | + return this.#suite.entryFile; | | 352 | + } | | 353 | + | 346 | 354 | get fullName() { | 347 | 355 | return getFullName(this.#suite); | 348 | 356 | } | @@ -357,7 +365,7 @@ class Test extends AsyncResource { | 357 | 365 | super('Test'); | 358 | 366 |
| 359 | 367 | let { fn, name, parent } = options; | 360 | | - const { concurrency, loc, only, timeout, todo, skip, signal, plan } = options; | | 368 | + const { concurrency, entryFile, loc, only, timeout, todo, skip, signal, plan } = options; | 361 | 369 |
| 362 | 370 | if (typeof fn !== 'function') { | 363 | 371 | fn = noop; | @@ -386,6 +394,7 @@ class Test extends AsyncResource { | 386 | 394 | this.runOnlySubtests = this.only; | 387 | 395 | this.childNumber = 0; | 388 | 396 | this.timeout = kDefaultTimeout; | | 397 | + this.entryFile = entryFile; | 389 | 398 | this.root = this; | 390 | 399 | this.hooks = { | 391 | 400 | __proto__: null, | @@ -406,6 +415,7 @@ class Test extends AsyncResource { | 406 | 415 | this.runOnlySubtests = !this.only; | 407 | 416 | this.childNumber = parent.subtests.length + 1; | 408 | 417 | this.timeout = parent.timeout; | | 418 | + this.entryFile = parent.entryFile; | 409 | 419 | this.root = parent.root; | 410 | 420 | this.hooks = { | 411 | 421 | __proto__: null, | |
Collapse file Copy file name to clipboard +52Lines changed: 52 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | +require('../common'); | | 3 | +const tmpdir = require('../common/tmpdir'); | | 4 | +const { strictEqual } = require('node:assert'); | | 5 | +const { writeFileSync } = require('node:fs'); | | 6 | +const { suite, test } = require('node:test'); | | 7 | + | | 8 | +tmpdir.refresh(); | | 9 | + | | 10 | +suite('suite', (t) => { | | 11 | + strictEqual(t.filePath, __filename); | | 12 | + | | 13 | + test('test', (t) => { | | 14 | + strictEqual(t.filePath, __filename); | | 15 | + | | 16 | + t.test('subtest', (t) => { | | 17 | + strictEqual(t.filePath, __filename); | | 18 | + | | 19 | + t.test('subsubtest', (t) => { | | 20 | + strictEqual(t.filePath, __filename); | | 21 | + }); | | 22 | + }); | | 23 | + }); | | 24 | +}); | | 25 | + | | 26 | +test((t) => { | | 27 | + strictEqual(t.filePath, __filename); | | 28 | +}); | | 29 | + | | 30 | +const importedTestFile = tmpdir.resolve('temp.js'); | | 31 | +writeFileSync(importedTestFile, ` | | 32 | + 'use strict'; | | 33 | + const { strictEqual } = require('node:assert'); | | 34 | + const { suite, test } = require('node:test'); | | 35 | +
| | 36 | + suite('imported suite', (t) => { | | 37 | + strictEqual(t.filePath, ${JSON.stringify(__filename)}); | | 38 | +
| | 39 | + test('imported test', (t) => { | | 40 | + strictEqual(t.filePath, ${JSON.stringify(__filename)}); | | 41 | +
| | 42 | + t.test('imported subtest', (t) => { | | 43 | + strictEqual(t.filePath, ${JSON.stringify(__filename)}); | | 44 | +
| | 45 | + t.test('imported subsubtest', (t) => { | | 46 | + strictEqual(t.filePath, ${JSON.stringify(__filename)}); | | 47 | + }); | | 48 | + }); | | 49 | + }); | | 50 | + }); | | 51 | +`); | | 52 | +require(importedTestFile); |
|