1+ import fs from 'node:fs'
2+ import path from 'node:path'
13import { expect , test } from 'vitest'
2- import { runVitest } from '../../test-utils'
4+ import { editFile , runVitest } from '../../test-utils'
5+ import { readInlineSnapshots } from './utils'
36
47test ( 'it should pass' , async ( ) => {
58 const { stdout, stderr } = await runVitest ( {
@@ -9,3 +12,126 @@ test('it should pass', async () => {
912 expect ( stdout ) . toContain ( '✓ custom-serializers.test.ts' )
1013 expect ( stderr ) . toBe ( '' )
1114} )
15+
16+ test ( 'empty serializer output' , async ( ) => {
17+ const root = path . join ( import . meta. dirname , 'fixtures/custom-serializers-empty' )
18+ const testFile = path . join ( root , 'basic.test.ts' )
19+ const snapshotFile = path . join ( root , '__snapshots__/basic.test.ts.snap' )
20+
21+ // clean slate
22+ fs . rmSync ( path . join ( root , '__snapshots__' ) , { recursive : true , force : true } )
23+ editFile ( testFile , s => s
24+ . replace ( / t o M a t c h I n l i n e S n a p s h o t \( ` [ ^ ` ] * ` / g, 'toMatchInlineSnapshot(' ) )
25+
26+ let result = await runVitest ( {
27+ root,
28+ update : 'all' ,
29+ } )
30+ expect ( result . stderr ) . toMatchInlineSnapshot ( `""` )
31+ expect ( result . errorTree ( ) ) . toMatchInlineSnapshot ( `
32+ Object {
33+ "basic.test.ts": Object {
34+ "file empty": "passed",
35+ "file whitespaces": "passed",
36+ "inline empty": "passed",
37+ "inline whitespaces": "passed",
38+ },
39+ }
40+ ` )
41+ expect ( fs . readFileSync ( snapshotFile , 'utf-8' ) ) . toMatchInlineSnapshot ( `
42+ "// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
43+
44+ exports[\`file empty 1\`] = \`\`;
45+
46+ exports[\`file whitespaces 1\`] = \` \`;
47+ "
48+ ` )
49+ expect ( readInlineSnapshots ( testFile ) ) . toMatchInlineSnapshot ( `
50+ "
51+ expect({ __unwrap__: "" }).toMatchInlineSnapshot(\`\`)
52+
53+ expect({ __unwrap__: " ".repeat(4) }).toMatchInlineSnapshot(\`\`)
54+ "
55+ ` )
56+
57+ result = await runVitest ( {
58+ root,
59+ update : 'none' ,
60+ } )
61+ expect ( result . stderr ) . toMatchInlineSnapshot ( `""` )
62+ expect ( result . errorTree ( ) ) . toMatchInlineSnapshot ( `
63+ Object {
64+ "basic.test.ts": Object {
65+ "file empty": "passed",
66+ "file whitespaces": "passed",
67+ "inline empty": "passed",
68+ "inline whitespaces": "passed",
69+ },
70+ }
71+ ` )
72+
73+ // TODO: snapshot comparison normalizes whitespaces. probably hard to fix.
74+ editFile ( testFile , s => s
75+ . replace ( `__unwrap__: " ".repeat(4)` , `__unwrap__: " ".repeat(8)` ) )
76+ result = await runVitest ( {
77+ root,
78+ update : 'none' ,
79+ } )
80+ expect ( result . stderr ) . toMatchInlineSnapshot ( `""` )
81+ expect ( result . errorTree ( ) ) . toMatchInlineSnapshot ( `
82+ Object {
83+ "basic.test.ts": Object {
84+ "file empty": "passed",
85+ "file whitespaces": "passed",
86+ "inline empty": "passed",
87+ "inline whitespaces": "passed",
88+ },
89+ }
90+ ` )
91+ expect ( fs . readFileSync ( snapshotFile , 'utf-8' ) ) . toMatchInlineSnapshot ( `
92+ "// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
93+
94+ exports[\`file empty 1\`] = \`\`;
95+
96+ exports[\`file whitespaces 1\`] = \` \`;
97+ "
98+ ` )
99+ expect ( readInlineSnapshots ( testFile ) ) . toMatchInlineSnapshot ( `
100+ "
101+ expect({ __unwrap__: "" }).toMatchInlineSnapshot(\`\`)
102+
103+ expect({ __unwrap__: " ".repeat(4) }).toMatchInlineSnapshot(\`\`)
104+ "
105+ ` )
106+
107+ result = await runVitest ( {
108+ root,
109+ update : 'all' ,
110+ } )
111+ expect ( result . stderr ) . toMatchInlineSnapshot ( `""` )
112+ expect ( result . errorTree ( ) ) . toMatchInlineSnapshot ( `
113+ Object {
114+ "basic.test.ts": Object {
115+ "file empty": "passed",
116+ "file whitespaces": "passed",
117+ "inline empty": "passed",
118+ "inline whitespaces": "passed",
119+ },
120+ }
121+ ` )
122+ expect ( fs . readFileSync ( snapshotFile , 'utf-8' ) ) . toMatchInlineSnapshot ( `
123+ "// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
124+
125+ exports[\`file empty 1\`] = \`\`;
126+
127+ exports[\`file whitespaces 1\`] = \` \`;
128+ "
129+ ` )
130+ expect ( readInlineSnapshots ( testFile ) ) . toMatchInlineSnapshot ( `
131+ "
132+ expect({ __unwrap__: "" }).toMatchInlineSnapshot(\`\`)
133+
134+ expect({ __unwrap__: " ".repeat(4) }).toMatchInlineSnapshot(\`\`)
135+ "
136+ ` )
137+ } )
0 commit comments