Skip to content
Open
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: 21 additions & 3 deletions testing/unstable_snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ import {
* @experimental **UNSTABLE**: New API, yet to be vetted.
*/
export interface InlineSnapshotOptions<T = unknown>
extends Pick<SnapshotOptions<T>, "msg" | "serializer"> {}
extends Pick<SnapshotOptions<T>, "msg" | "serializer"> {
/**
* Function frame to start backtrace from.
* The source code location to modify will be decided from the stack that comes after this function.
*
* This is only relevant if you are wrapping the call to {@linkcode assertInlineSnapshot}.
*/
frame?: typeof assertInlineSnapshot;
}

interface ErrorLocation {
lineNumber: number;
Expand Down Expand Up @@ -169,6 +177,10 @@ globalThis.addEventListener("unload", () => {
*
* Type parameter can be specified to ensure values under comparison have the same type.
*
* This function discovers the caller's source location by doing a stacktrace.
* If you wraps this function, make sure to specifiy the correct frame location in
* {@linkcode InlineSnapshotOptions.frame}.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Usage
Expand Down Expand Up @@ -255,7 +267,11 @@ export function assertInlineSnapshot(
};
};
// Capture the stack that comes after this function.
Error.captureStackTrace(stackCatcher, assertInlineSnapshot);
Error.captureStackTrace(
stackCatcher,
// Start backtrace from this frame, otherwise the call location will be wrong.
options.frame ?? assertInlineSnapshot,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth a one-line comment noting that callers (specifically createAssertInlineSnapshot) pass their own wrapper function as frame so V8 skips it during stack capture. Otherwise the ?? here looks like a generic option-passthrough and the relationship to the bug fix isn't obvious to a future reader.

);
// Forcibly access the stack, and note it down
const request = stackCatcher.stack;
if (request !== null) {
Expand Down Expand Up @@ -303,12 +319,14 @@ export function createAssertInlineSnapshot<T>(
options: InlineSnapshotOptions<T>,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: a one-liner in this JSDoc mentioning that the wrapper preserves the caller's source location for --update would help discoverability of the fix.

baseAssertSnapshot: typeof assertInlineSnapshot = assertInlineSnapshot,
): typeof assertInlineSnapshot {
return function (
return function frame(
actual: T,
expectedSnapshot: string,
messageOrOptions?: string | InlineSnapshotOptions<T>,
) {
const mergedOptions: InlineSnapshotOptions<T> = {
// Ordering matters here to allow outer options overriding frame.
frame,
...options,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ordering { frame, ...options, ...messageOrOptions } is load-bearing and worth a comment: it makes the wrapper's own frame the default while still allowing an outer wrapper to override frame via the forwarded options — that's what enables nested createAssertInlineSnapshot wrappers to compose correctly. Without a note, a well-meaning refactor could reorder these spreads and silently break nesting.

...(typeof messageOrOptions === "string"
? { msg: messageOrOptions }
Expand Down
50 changes: 48 additions & 2 deletions testing/unstable_snapshot_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ Deno.test("assertInlineSnapshot() counts lines and columns like V8", async () =>
await Deno.writeTextFile(
countTestFile,
`import { assertInlineSnapshot } from "${SNAPSHOT_MODULE_URL}";
\n \r \n\r \r\n \u2028 \u2029
\n \r \n\r \r\n \u2028 \u2029\u0020
Deno.test("format", async () => {
/* 🐈‍⬛🇦🇶 */ assertInlineSnapshot( "hello world", "" );
});`,
Expand All @@ -174,7 +174,7 @@ Deno.test("format", async () => {
assertEquals(
await Deno.readTextFile(countTestFile),
`import { assertInlineSnapshot } from "${SNAPSHOT_MODULE_URL}";
\n \r \n\r \r\n \u2028 \u2029
\n \r \n\r \r\n \u2028 \u2029\u0020
Deno.test("format", async () => {
/* 🐈‍⬛🇦🇶 */ assertInlineSnapshot( "hello world", \`"hello world"\` );
});`,
Expand All @@ -193,3 +193,49 @@ Deno.test("createAssertInlineSnapshot()", () => {
`This green text has had its colors stripped`,
);
});

Deno.test("createAssertInlineSnapshot() writes to the correct location", async () => {
if (!LINT_SUPPORTED) return;

const tempDir = await Deno.makeTempDir();
const testFile = join(tempDir, "create_test.ts");
try {
await Deno.writeTextFile(
testFile,
`import { createAssertInlineSnapshot } from "${SNAPSHOT_MODULE_URL}";

const assertInlineSnapshot = createAssertInlineSnapshot({});

Deno.test("format", () => {
assertInlineSnapshot( "hello world", "" );
});`,
);

const command = new Deno.Command(Deno.execPath(), {
args: [
"test",
"--no-lock",
"--allow-read",
"--allow-write",
tempDir,
"--",
"--update",
"--no-format",
],
});
await command.output();

assertEquals(
await Deno.readTextFile(testFile),
`import { createAssertInlineSnapshot } from "${SNAPSHOT_MODULE_URL}";

const assertInlineSnapshot = createAssertInlineSnapshot({});

Deno.test("format", () => {
assertInlineSnapshot( "hello world", \`"hello world"\` );
});`,
);
} finally {
await Deno.remove(tempDir, { recursive: true });
}
});
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good regression test — exactly the right shape. Without the fix this would fail because the wrapper would try to update the literal at unstable_snapshot.ts:createAssertInlineSnapshot's line rather than the user's test file.