Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline snapshots with line breaks are unstable #1134

Closed
6 tasks done
AriPerkkio opened this issue Apr 10, 2022 · 1 comment · Fixed by #1232
Closed
6 tasks done

Inline snapshots with line breaks are unstable #1134

AriPerkkio opened this issue Apr 10, 2022 · 1 comment · Fixed by #1232

Comments

@AriPerkkio
Copy link
Member

AriPerkkio commented Apr 10, 2022

Describe the bug

Test below keeps failing when run. It passes right after the snapshot is updated but fails immediately on the next run. Snapshot update doesn't actually change any content. I think vitest doesn't handle line changes properly when comparing inline snapshots.

import { expect, test } from 'vitest';

/*
 * - npm run test
 * - Error -> Snapshot has changed. Press u
 * - Snapshot is updated, test passes. Press enter to re-run
 * - Error -> Snapshot has changed. Repeat previous steps...
 */

test('unstable snapshot', () => {
  expect(() => {
    throw new Error(['First line', 'Second line'].join('\n'));
  }).toThrowErrorMatchingInlineSnapshot(`
    "First line
    Second line"
  `);
});
vitest-unstable-snapshot.mp4

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-kxavky?file=test/basic.test.ts

System Info

Default Stackblitz environment.

Used Package Manager

npm

Validations

@AriPerkkio
Copy link
Member Author

match({
testName,
received,
key,
inlineSnapshot,

┌────────────────┬───────────────────────────────────────────┐
│ inlineSnapshot │ '\n    "First line\n    Second line"\n  ' │
│    received    │         'First line\nSecond line'         │
└────────────────┴───────────────────────────────────────────┘

const pass = expectedTrimmed === prepareExpected(receivedSerialized)

const pass
  = expectedTrimmed //                       "First line\n    Second line"
  === prepareExpected(receivedSerialized) // "First line\nSecond line"

Before comparing the receivedSerialized, all newlines inside the quotes should be indented with required amount of spaces, 4 in this case.

'First line\nSecond line'
to 
'First line\n    Second line'

This padding would ensure that intentional indention would work, e.g.

expect('Hello \n    world').toMatchInlineSnapshot...

@github-actions github-actions bot locked and limited conversation to collaborators Jun 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant