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 are written at wrong positions if within large source files with CRLF end of line #2959

Closed
6 tasks done
suXinjke opened this issue Mar 4, 2023 · 0 comments · Fixed by #3032
Closed
6 tasks done

Comments

@suXinjke
Copy link

suXinjke commented Mar 4, 2023

Describe the bug

Given the large source test file with CRLF EOL in it, the inline snapshot functionality may overwrite snapshots located above the targeted ones. This happens because of the function that converts line:column to byte offset - it doesn't account for CR properly.

Bug originates from code that relies on this regex

The bigger your source code file is, the higher the risk you will encounter this issue. Without research I would have no idea it was EOL to blame.

The issue is demonstrated below:

issue

Reproduction

I have uploaded the demonstrated example here: https://github.com/suXinjke/vitest/tree/crlf-issue
Commits include commentary on the bug, marks portions where the bug may happen too, and include a possible fix.

suXinjke/vitest@116b7db - example and commentary
suXinjke/vitest@39a4fe2 - possible fix

  • Checkout onto suXinjke/vitest@116b7db
  • Open vitest\test\snapshots\test\snapshots-crlf.test.ts, make sure EOL is set to CRLF. Convert it to CRLF if that's not the case.
  • pnpm run test:issue
  • You will be prompted to update the snapshots, do so
  • Wrong snapshot will be updated

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (12) x64 AMD Ryzen 5 5600X 6-Core Processor
    Memory: 1.41 GB / 15.88 GB
  Binaries:
    Node: 16.18.1 - D:\IDE\node\node.EXE
    npm: 8.5.2 - D:\IDE\node\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (110.0.1587.57)    
    Internet Explorer: 11.0.19041.1566

Used Package Manager

npm

Validations

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.

2 participants