From 2ce7c69fce9f281563f49971058ffce73ff4b45a Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sun, 14 Jul 2024 23:37:10 -0400 Subject: [PATCH] test_runner: add context.filePath This commit adds a filePath getter to the TestContext and SuiteContext classes. This allows a context to be mapped back to the original test file that created it, even if it was imported from another file. This is useful for mapping features like test snapshots to the correct test file. This is also prep work for supporting running test files in the test runner process. PR-URL: https://github.com/nodejs/node/pull/53853 Reviewed-By: Moshe Atlow Reviewed-By: Chemi Atlow Reviewed-By: Benjamin Gruenbaum --- doc/api/test.md | 20 +++++++++ lib/internal/test_runner/harness.js | 2 +- lib/internal/test_runner/test.js | 12 ++++- test/parallel/test-runner-test-filepath.js | 52 ++++++++++++++++++++++ 4 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-runner-test-filepath.js diff --git a/doc/api/test.md b/doc/api/test.md index 716fe8c5f62..e0d4bee17a7 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -3204,6 +3204,16 @@ test('top level test', (t) => { }); ``` +### `context.filePath` + + + +The absolute path of the test file that created the current test. If a test file +imports additional modules that generate tests, the imported tests will return +the path of the root test file. + ### `context.fullName` + +The absolute path of the test file that created the current suite. If a test +file imports additional modules that generate suites, the imported suites will +return the path of the root test file. + ### `context.name`