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

Snapshots incorrectly treat CRLF and LF the same #7305

Open
6 tasks done
melusc opened this issue Jan 20, 2025 · 1 comment
Open
6 tasks done

Snapshots incorrectly treat CRLF and LF the same #7305

melusc opened this issue Jan 20, 2025 · 1 comment

Comments

@melusc
Copy link

melusc commented Jan 20, 2025

Describe the bug

Vitest treats CRLF and LF as the same in snapshots which leads to the wrong snapshot being used. E.g. if a string has CRLF it'll incorrectly be normalised to LF.

import {test, expect} from 'vitest';

test.for([
	'a\nb', // ["a", "\n", "b"], snapshot will have ["a", "\n", "b"]
	'a\r\nb', // ["a", "\r", "\n", "b"], snapshot will have ["a", "\n", "\n", "b"]
])('"%s".split("")', (input) => {
	expect(input.split("")).toMatchSnapshot();
})

On Windows it correctly generates two snapshots for both. But then when it compares the snapshot on a second run this test will fail because the line endings aren't right.

On Ubuntu it writes the first snapshot to the file. Then it incorrectly uses the first snapshot for the second test and fails.

Reproduction

https://github.com/melusc/vitest-snapshot-crlf

You can see how it behaves on Windows or Ubuntu in the Github Actions logs: https://github.com/melusc/vitest-snapshot-crlf/actions/runs/12869505983

System Info

System:
    OS: Windows 11 10.0.26100
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
    Memory: 770.81 MB / 7.84 GB
  Binaries:
    Node: 23.6.0 - C:\nvm4w\nodejs\node.EXE
    Yarn: 4.6.0 - C:\nvm4w\nodejs\yarn.CMD
    npm: 10.9.2 - C:\nvm4w\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (124.0.2478.105)
  npmPackages:
    vitest: ^3.0.2 => 3.0.2

Used Package Manager

yarn

Validations

@melusc melusc changed the title Snapshots ignore CRLF Snapshots incorrectly treat CRLF and LF the same Jan 20, 2025
@melusc
Copy link
Author

melusc commented Jan 20, 2025

Workaround would be:

test.for([
	'a\nb', // ["a", "\n", "b"]
	'a\r\nb', // ["a", "\r", "\n", "b"]
-])('"%s".split("")', (input) => {
+])('%j.split("")', (input) => {
-	expect(input.split("")).toMatchSnapshot();
+	expect(JSON.stringify(input.split(""))).toMatchSnapshot();
})

I think an actual solution would be in this direction. Instead of writing actual CRs and LFs in the title and snapshot, write \r and \n so that CRLF cannot be normalised to LF.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant