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

Odd behavior with toThrowErrorMatchingInlineSnapshot with multiline error message #4662

Closed
6 tasks done
hi-ogawa opened this issue Dec 5, 2023 · 0 comments · Fixed by #4663
Closed
6 tasks done

Odd behavior with toThrowErrorMatchingInlineSnapshot with multiline error message #4662

hi-ogawa opened this issue Dec 5, 2023 · 0 comments · Fixed by #4663

Comments

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Dec 5, 2023

Describe the bug

While updating to vitest v1 on my project hi-ogawa/js-utils#185 (comment), I noticed that the snapshot generated by toThrowErrorMatchingInlineSnapshot for multiline error message becomes mismatch on next run.

Here is a repro https://stackblitz.com/edit/vitest-dev-vitest-fmwjqc?file=test%2Fbasic.test.ts and the failing snapshot is following:

test('bad', () => {
  expect(() => {
    throw new Error('hello\nworld');
  }).toThrowErrorMatchingInlineSnapshot(`
    [Error: hello
    world]
  `);
});
 FAIL  test/repro.test.ts > bad
Error: Snapshot `bad 1` mismatched

- Expected
+ Received

  [Error: hello
-     world]
+ world]

I added similar cases in the repro. Interestingly I found that directly taking a snapshot Error instance via toMatchInlineSnapshot seems to be okay:

  expect(new Error('hello\nworld')).toMatchInlineSnapshot(`
    [Error: hello
    world]
  `);

It's most likely that #4396 introduced this issue, so hopefully I can figure it out myself, but if maintainers have some idea to fix (or intend to revert the PR?), then I'm happy with that as well.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-fmwjqc?file=test%2Fbasic.test.ts

  • it fails on initial run by npm run test:ui
  • it passes after u shortcut
  • it fails when re-running after <Enter>

I also made a repro with v1.0.0-beta.3 which is the last version before #4396 and there's no issue with this version:
https://stackblitz.com/edit/vitest-dev-vitest-tkskse?file=test%2Frepro.test.ts

System Info

(Stackblitz)

  System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.4.2 - /usr/local/bin/npm
    pnpm: 8.10.5 - /usr/local/bin/pnpm
  npmPackages:
    @vitest/ui: latest => 1.0.1 
    vite: latest => 5.0.5 
    vitest: latest => 1.0.1

Used Package Manager

npm

Validations

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

Successfully merging a pull request may close this issue.

1 participant