Skip to content

fix(testing/unstable): use the correct function frame in inline snapshot#7150

Open
mochaaP wants to merge 1 commit into
denoland:mainfrom
mcha-forks:mochaa-fix-snapshot-writing
Open

fix(testing/unstable): use the correct function frame in inline snapshot#7150
mochaaP wants to merge 1 commit into
denoland:mainfrom
mcha-forks:mochaa-fix-snapshot-writing

Conversation

@mochaaP
Copy link
Copy Markdown

@mochaaP mochaaP commented May 21, 2026

Before the fix, it will always write to unstable_snapshot.ts:createAssertInlineSnapshot.

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 21, 2026

CLA assistant check
All committers have signed the CLA.

@mochaaP mochaaP force-pushed the mochaa-fix-snapshot-writing branch from 140707c to 5126a0a Compare May 21, 2026 20:23
@codecov
Copy link
Copy Markdown

codecov Bot commented May 21, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.61%. Comparing base (95a1e2e) to head (4f952ad).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7150   +/-   ##
=======================================
  Coverage   94.61%   94.61%           
=======================================
  Files         634      634           
  Lines       51843    51846    +3     
  Branches     9346     9347    +1     
=======================================
+ Hits        49050    49053    +3     
  Misses       2218     2218           
  Partials      575      575           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mochaaP mochaaP force-pushed the mochaa-fix-snapshot-writing branch from 5126a0a to 4f952ad Compare May 21, 2026 21:00
Copy link
Copy Markdown
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

Nice catch — small, well-targeted fix for a real bug in createAssertInlineSnapshot.

Before this PR, Error.captureStackTrace(stackCatcher, assertInlineSnapshot) only skipped the inner assertInlineSnapshot frame, so the wrapper returned by createAssertInlineSnapshot was reported as the caller and the lint plugin tried to update the snapshot literal inside unstable_snapshot.ts itself. The named function expression + options.frame indirection is the right idiom, and as a bonus the merge order makes nested wrappers compose (the outer wrapper's frame rides down through messageOrOptions).

A few inline nits below, plus one comment on an unrelated whitespace change in the test file. LGTM once those are addressed.

Comment thread testing/unstable_snapshot.ts Outdated
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.
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.

Small grammar nit: will be the decidedwill be decided. Consider rewording slightly for clarity, e.g.:

The source code location to modify is taken from the stack frame that comes after this function.

Error.captureStackTrace(stackCatcher, assertInlineSnapshot);
Error.captureStackTrace(
stackCatcher,
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.

) {
const mergedOptions: InlineSnapshotOptions<T> = {
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.

@@ -303,12 +315,13 @@ 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.

Comment thread testing/unstable_snapshot_test.ts Outdated
countTestFile,
`import { assertInlineSnapshot } from "${SNAPSHOT_MODULE_URL}";
\n \r \n\r \r\n \u2028 \u2029
\n \r \n\r \r\n \u2028 \u2029
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.

This trailing-whitespace change (and the mirrored one on line 177) is unrelated to the frame fix. Since the literal string here is the actual input to the "counts lines and columns like V8" test, can you confirm removing the trailing space doesn't alter what's being exercised (e.g. the column count after \u2029)? If it's a formatter-required change, fine — but worth calling out in the PR body, or splitting into its own commit.

} 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.

@mochaaP mochaaP force-pushed the mochaa-fix-snapshot-writing branch from 4f952ad to 875f6e7 Compare May 26, 2026 11:11
@mochaaP
Copy link
Copy Markdown
Author

mochaaP commented May 26, 2026

all reviews resolved.

@mochaaP mochaaP requested a review from bartlomieju May 26, 2026 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants