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
19 changes: 16 additions & 3 deletions testing/unstable_snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ 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 the decided from the stack that comes after this function.
*
* This is only relevant if you are wrapping the call to {@linkcode assertInlineSnapshot}.
*/
// deno-lint-ignore ban-types
frame?: Function;
}

interface ErrorLocation {
lineNumber: number;
Expand Down Expand Up @@ -255,7 +264,10 @@ export function assertInlineSnapshot(
};
};
// Capture the stack that comes after this function.
Error.captureStackTrace(stackCatcher, assertInlineSnapshot);
Error.captureStackTrace(
stackCatcher,
options.frame ?? assertInlineSnapshot,
);
// Forcibly access the stack, and note it down
const request = stackCatcher.stack;
if (request !== null) {
Expand Down Expand Up @@ -303,12 +315,13 @@ export function createAssertInlineSnapshot<T>(
options: InlineSnapshotOptions<T>,
baseAssertSnapshot: typeof assertInlineSnapshot = assertInlineSnapshot,
): typeof assertInlineSnapshot {
return function (
return function frame(
actual: T,
expectedSnapshot: string,
messageOrOptions?: string | InlineSnapshotOptions<T>,
) {
const mergedOptions: InlineSnapshotOptions<T> = {
frame,
...options,
...(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
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
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 });
}
});
Loading