node/test/parallel/test-runner-test-filepath.js
cjihrig 2ce7c69fce 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 <moshe@atlow.co.il>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
2024-07-17 07:01:21 +00:00

53 lines
1.3 KiB
JavaScript

'use strict';
require('../common');
const tmpdir = require('../common/tmpdir');
const { strictEqual } = require('node:assert');
const { writeFileSync } = require('node:fs');
const { suite, test } = require('node:test');
tmpdir.refresh();
suite('suite', (t) => {
strictEqual(t.filePath, __filename);
test('test', (t) => {
strictEqual(t.filePath, __filename);
t.test('subtest', (t) => {
strictEqual(t.filePath, __filename);
t.test('subsubtest', (t) => {
strictEqual(t.filePath, __filename);
});
});
});
});
test((t) => {
strictEqual(t.filePath, __filename);
});
const importedTestFile = tmpdir.resolve('temp.js');
writeFileSync(importedTestFile, `
'use strict';
const { strictEqual } = require('node:assert');
const { suite, test } = require('node:test');
suite('imported suite', (t) => {
strictEqual(t.filePath, ${JSON.stringify(__filename)});
test('imported test', (t) => {
strictEqual(t.filePath, ${JSON.stringify(__filename)});
t.test('imported subtest', (t) => {
strictEqual(t.filePath, ${JSON.stringify(__filename)});
t.test('imported subsubtest', (t) => {
strictEqual(t.filePath, ${JSON.stringify(__filename)});
});
});
});
});
`);
require(importedTestFile);