@@ -48,6 +48,176 @@ test("if file has import.meta.glob, it's not cached", async () => {
4848 ` )
4949} )
5050
51+ test ( "if file has import.meta.glob with a type argument, it's not cached" , async ( ) => {
52+ const structure = {
53+ 'src/a.js' : 'export default 1' ,
54+ 'src/b.js' : 'export default 2' ,
55+ 'glob.test.ts' : /* ts */ `
56+ import { expect, inject, test } from 'vitest'
57+
58+ const files = import.meta.glob<{ default: number }>('./src/*.js', { eager: true })
59+
60+ test('glob', () => {
61+ expect(Object.keys(files)).toEqual(inject('files'))
62+ })
63+ ` ,
64+ }
65+ const config = {
66+ fsModuleCache : true ,
67+ fsModuleCachePath : './node_modules/.vitest-fs-cache' ,
68+ }
69+
70+ const cold = await runInlineTests ( structure , {
71+ ...config ,
72+ provide : { files : [ './src/a.js' , './src/b.js' ] } ,
73+ } )
74+
75+ expect ( cold . stderr ) . toBe ( '' )
76+ expect ( cold . errorTree ( ) ) . toMatchInlineSnapshot ( `
77+ {
78+ "glob.test.ts": {
79+ "glob": "passed",
80+ },
81+ }
82+ ` )
83+ await cold . ctx ?. close ( )
84+
85+ cold . fs . createFile ( 'src/c.js' , 'export default 3' )
86+
87+ const warm = await runVitest ( {
88+ root : cold . root ,
89+ ...config ,
90+ provide : { files : [ './src/a.js' , './src/b.js' , './src/c.js' ] } ,
91+ } )
92+
93+ expect ( warm . stderr ) . toBe ( '' )
94+ expect ( warm . errorTree ( ) ) . toMatchInlineSnapshot ( `
95+ {
96+ "glob.test.ts": {
97+ "glob": "passed",
98+ },
99+ }
100+ ` )
101+ } )
102+
103+ test ( "if file has a dynamic import with a template literal, it's not cached" , async ( ) => {
104+ const structure = {
105+ 'src/a.js' : `export default 'a'` ,
106+ 'src/b.js' : `export default 'b'` ,
107+ 'load.test.js' : /* js */ `
108+ import { expect, inject, test } from 'vitest'
109+
110+ const load = (name) => import(\`./src/\${name}.js\`)
111+
112+ test('load', async () => {
113+ const names = inject('names')
114+ const modules = await Promise.all(names.map(load))
115+ expect(modules.map((m) => m.default)).toEqual(names)
116+ })
117+ ` ,
118+ }
119+ const config = {
120+ fsModuleCache : true ,
121+ fsModuleCachePath : './node_modules/.vitest-fs-cache' ,
122+ }
123+
124+ const cold = await runInlineTests ( structure , {
125+ ...config ,
126+ provide : { names : [ 'a' , 'b' ] } ,
127+ } )
128+
129+ expect ( cold . stderr ) . toBe ( '' )
130+ expect ( cold . errorTree ( ) ) . toMatchInlineSnapshot ( `
131+ {
132+ "load.test.js": {
133+ "load": "passed",
134+ },
135+ }
136+ ` )
137+ await cold . ctx ?. close ( )
138+
139+ cold . fs . createFile ( 'src/c.js' , `export default 'c'` )
140+
141+ const warm = await runVitest ( {
142+ root : cold . root ,
143+ ...config ,
144+ provide : { names : [ 'a' , 'b' , 'c' ] } ,
145+ } )
146+
147+ expect ( warm . stderr ) . toBe ( '' )
148+ expect ( warm . errorTree ( ) ) . toMatchInlineSnapshot ( `
149+ {
150+ "load.test.js": {
151+ "load": "passed",
152+ },
153+ }
154+ ` )
155+ } )
156+
157+ test ( "if file has new URL() with a template literal, it's not cached" , async ( ) => {
158+ const structure = {
159+ 'assets/a.png' : '' ,
160+ 'assets/b.png' : '' ,
161+ 'client-env.js' : /* js */ `
162+ export default {
163+ name: 'client-env',
164+ viteEnvironment: 'client',
165+ setup() {
166+ return { teardown() {} }
167+ },
168+ }
169+ ` ,
170+ 'url.test.js' : /* js */ `
171+ import { expect, inject, test } from 'vitest'
172+
173+ const getUrl = (name) => new URL(\`./assets/\${name}.png\`, import.meta.url)
174+
175+ test('url', () => {
176+ const names = inject('names')
177+ const files = names.map((name) => getUrl(name).pathname.split('/').pop())
178+ expect(files).toEqual(names.map((name) => \`\${name}.png\`))
179+ })
180+ ` ,
181+ }
182+ const config = {
183+ environment : './client-env.js' ,
184+ fsModuleCache : true ,
185+ fsModuleCachePath : './node_modules/.vitest-fs-cache' ,
186+ }
187+
188+ const cold = await runInlineTests ( structure , {
189+ ...config ,
190+ provide : { names : [ 'a' , 'b' ] } ,
191+ } )
192+
193+ expect ( cold . stderr ) . toBe ( '' )
194+ expect ( cold . errorTree ( ) ) . toMatchInlineSnapshot ( `
195+ {
196+ "url.test.js": {
197+ "url": "passed",
198+ },
199+ }
200+ ` )
201+ await cold . ctx ?. close ( )
202+
203+ cold . fs . createFile ( 'assets/c.png' , '' )
204+
205+ const warm = await runVitest ( {
206+ root : cold . root ,
207+ ...config ,
208+ provide : { names : [ 'a' , 'b' , 'c' ] } ,
209+ } )
210+
211+ expect ( warm . stderr ) . toBe ( '' )
212+ expect ( warm . errorTree ( ) ) . toMatchInlineSnapshot ( `
213+ {
214+ "url.test.js": {
215+ "url": "passed",
216+ },
217+ }
218+ ` )
219+ } )
220+
51221test ( 'if no cache key generator is defined, the hash is invalid' , async ( ) => {
52222 process . env . REPLACED = 'value1'
53223
@@ -409,5 +579,6 @@ test('if cache key generator bails out, the file is not cached', async () => {
409579declare module 'vitest' {
410580 export interface ProvidedContext {
411581 generated : string [ ]
582+ names : string [ ]
412583 }
413584}
0 commit comments