deno_std icon indicating copy to clipboard operation
deno_std copied to clipboard

assertSnapshot fails to match carriage return

Open OzzieOrca opened this issue 1 year ago • 4 comments

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

  1. Create a file (crlf.test.ts) with the contents
    import { assertSnapshot } from 'https://deno.land/[email protected]/testing/snapshot.ts';
    
    Deno.test('CR failure', async (t) => {
      await assertSnapshot(t, '\r');
    });
    
  2. Run deno test --allow-read --allow-write cr.test.ts -- --update which returns
    running 1 test from ./cr.test.ts
    CR failure ... ok (2ms)
    ------- output -------
    
    > 1 snapshot updated.
    
    ok | 1 passed | 0 failed (4ms)
    
    and saves __snapshots__/cr.test.ts.snap with
    export const snapshot = {};
    
    snapshot[`CR failure 1`] = `
    "
    "
    `;
    
  3. Run deno test --allow-read cr.test.ts which 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

OzzieOrca avatar Jan 21 '24 03:01 OzzieOrca

I'm taking a look at this one

javihernant avatar Feb 23 '24 09:02 javihernant

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 avatar Feb 23 '24 12:02 javihernant

@javihernant Interesting finding! Can you create an issue in denoland/deno with a minimal repro example?

kt3k avatar Feb 24 '24 13:02 kt3k

hmm looks like Deno ignoring \r in string literal is not a bug. Maybe we should escape \r when serializing it

kt3k avatar Mar 16 '24 22:03 kt3k

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.

OzzieOrca avatar Jun 12 '24 17:06 OzzieOrca