deno_std
deno_std copied to clipboard
assertSnapshot fails to match carriage return
Describe the bug
If you pass the CR character \r to assertSnapshot, it saves the snapshot fine but then immediately fails to match the snapshot.
I ran into this by trying to snapshot the output of stringify(...) from https://deno.land/[email protected]/csv/mod.ts which apparently output CRLFs (\r\n).
Steps to Reproduce
- Create a file (
crlf.test.ts) with the contentsimport { assertSnapshot } from 'https://deno.land/[email protected]/testing/snapshot.ts'; Deno.test('CR failure', async (t) => { await assertSnapshot(t, '\r'); }); - Run
deno test --allow-read --allow-write cr.test.ts -- --updatewhich returns
and savesrunning 1 test from ./cr.test.ts CR failure ... ok (2ms) ------- output ------- > 1 snapshot updated. ok | 1 passed | 0 failed (4ms)__snapshots__/cr.test.ts.snapwithexport const snapshot = {}; snapshot[`CR failure 1`] = ` " " `; - Run
deno test --allow-read cr.test.tswhich returns:running 1 test from ./cr.test.ts CR failure ... FAILED (6ms) ERRORS CR failure => ./cr.test.ts:3:6 error: AssertionError: Snapshot does not match: [Diff] Actual / Expected - " + " " throw new AssertionError( ^ at assertSnapshot (https://deno.land/[email protected]/testing/snapshot.ts:591:11) at async file:///home/scotty/code/Toptal/FoodNoms/macrosai-supabase/cr.test.ts:4:3 FAILURES CR failure => ./cr.test.ts:3:6 FAILED | 0 passed | 1 failed (8ms) error: Test failed
Expected behavior
The test should succeed after the snapshot has been updated.
Environment
- OS: Ubuntu 22.04.3 LTS
- deno version: 1.39.1
- std version: 0.212.0
I'm taking a look at this one
The culprit seems to be here. When .ts.snap is written, an actual "\r" is written to the file. However, when importing it reads a "\n". import bugs have to be fixed in the actual runtime (not in deno_std), right? Note: this one was fun to find! ^^
@javihernant Interesting finding! Can you create an issue in denoland/deno with a minimal repro example?
hmm looks like Deno ignoring \r in string literal is not a bug. Maybe we should escape \r when serializing it
Sounds like assertSnapshot should strip (or escape somehow?) \r characters before comparing and storing snapshots. At least that's my takeaway from https://github.com/denoland/deno/issues/22575#issuecomment-2162989848.