Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: expose file & fullName props on TestContext (for snapshots) #53179

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,30 @@
return this.#test.signal;
}

/**
* Get the path of the current test file.
* @example '/tmp/test.js'
*/
get file() {
return this.#test.loc.file;
}

/**
* Get the name for the current context and all ancestors (outputs a string from root to tip).
* @example 'Animals › Cat › sleep() should be possible at any time'

Check failure on line 227 in lib/internal/test_runner/test.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Non-ASCII character '›' detected
*/
get fullName() {
let fullName = this.#test.name;
let parent = this.#test.parent;

while (parent.parent) { // `null` when at the root which has no name, so abort when we get there
fullName = `${parent.name} › ${fullName}`;
({ parent } = parent);
}

return fullName;
}

get name() {
return this.#test.name;
}
Expand Down
25 changes: 25 additions & 0 deletions test/parallel/test-runner-snapshot-props.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';
require('../common');

const assert = require('node:assert');
const { resolve } = require('node:path');
const { describe, test } = require('node:test');


// These props are needed to support jest-like snapshots (which is not itself a feature in Core).

const rootName = 'props for snapshots';
describe(rootName, () => {
test('test context has "file" property', (ctx) => {
assert.strictEqual(ctx.file, resolve(__filename));
});

const nestedName = '"fullName" contains both case and ancestor names';
test(nestedName, (ctx) => {
assert.match(ctx.fullName, new RegExp(rootName));
assert.match(ctx.fullName, new RegExp(nestedName));

// Ensure root appears before tip
assert.ok(ctx.fullName.indexOf(rootName) < ctx.fullName.indexOf(nestedName));
});
});
Loading