You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When performing snapshot testing with strings using expect("some string").toMatchSnapshot(), I expect that any difference in the string should be detected. However, the input string is modified by trimming whitespace and adjusting indentation.
For instance, both of the following lines succeed for the same example.txt with the string "Whitespace gets trimmed", even though one might expect the second line to fail due to additional whitespace:
expect(`Whitespace gets trimmed`).toMatchFileSnapshot('example.txt');expect(` Whitespace gets trimmed `).toMatchFileSnapshot('example.txt');// Should fail, but succeeds
The code in Vitest responsible for modifying the string is located here:
This also causes problems with the diff view in the console output.
Given a file example.yml with a multiline string that contains a line with the string " } " (the space after } is important)
constyml=fs.readFileSync('test/example.yml',{encoding: 'utf8',flag: 'r',});// This succeeds as expectedawaitexpect(yml).toMatchFileSnapshot('example.yml');constmodifiedYml=yml.replaceAll('world','mars');// This fails as expected, but the diff is brokenawaitexpect(modifiedYml).toMatchFileSnapshot('example.yml');// When not using file snapshots, the error diffs looks as expected// (Uncomment previous expect to see output)expect(modifiedYml).toEqual(yml);
produces the following console output:
Notice that the green "expected" part of the diff is "indentation-adjusted", while the red "received" part is not. This does not happen when using toEqual directly, where the diff looks as expected:
I ran into this problem when testing Kubernetes artifacts generated using cdk8s.
I am also seeing this behavior. For my project it's a bit of a deal breaker, because it's a Prettier plugin and every newline/indentation is important to check for regressions.
This is a very unexpected behavior, and as far as I can tell there's no way currently to avoid that code path in Vitest. Please correct me if I'm wrong.
@dangmai if it helps, we are working around it right now using patch-package with the following patch for vitest 2.1.3. The patch will obviously will need to be updated for each vitest upgrade, but it's small enough that it's feasible until the fix arrives.
Describe the bug
When performing snapshot testing with strings using
expect("some string").toMatchSnapshot()
, I expect that any difference in the string should be detected. However, the input string is modified by trimming whitespace and adjusting indentation.For instance, both of the following lines succeed for the same
example.txt
with the string"Whitespace gets trimmed"
, even though one might expect the second line to fail due to additional whitespace:The code in Vitest responsible for modifying the string is located here:
vitest/packages/snapshot/src/port/utils.ts
Lines 178 to 206 in bd83f6c
This also causes problems with the diff view in the console output.
Given a file
example.yml
with a multiline string that contains a line with the string" } "
(the space after}
is important)the following test
produces the following console output:
Notice that the green "expected" part of the diff is "indentation-adjusted", while the red "received" part is not. This does not happen when using
toEqual
directly, where the diff looks as expected:I ran into this problem when testing Kubernetes artifacts generated using cdk8s.
Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-ftiqce?file=test%2Fbasic.test.ts
System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: