fix(testing/unstable): use the correct function frame in inline snapshot#7150
fix(testing/unstable): use the correct function frame in inline snapshot#7150mochaaP wants to merge 1 commit into
Conversation
140707c to
5126a0a
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
5126a0a to
4f952ad
Compare
bartlomieju
left a comment
There was a problem hiding this comment.
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.
| 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. |
There was a problem hiding this comment.
Small grammar nit: will be the decided → will 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, |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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>, | |||
There was a problem hiding this comment.
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.
| countTestFile, | ||
| `import { assertInlineSnapshot } from "${SNAPSHOT_MODULE_URL}"; | ||
| \n \r \n\r \r\n \u2028 \u2029 | ||
| \n \r \n\r \r\n \u2028 \u2029 |
There was a problem hiding this comment.
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 }); | ||
| } | ||
| }); |
There was a problem hiding this comment.
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.
Signed-off-by: Zephyr Lykos <git@mochaa.ws>
4f952ad to
875f6e7
Compare
|
all reviews resolved. |
Before the fix, it will always write to
unstable_snapshot.ts:createAssertInlineSnapshot.