1
- import fs from 'node:fs'
2
1
import path from 'node:path'
3
- import { describe , expect , test , vi } from 'vitest'
2
+ import { describe , expect , test } from 'vitest'
4
3
import { resolveConfig } from '../../config'
5
4
import type { InlineConfig } from '../../config'
6
5
import {
@@ -57,27 +56,20 @@ describe('search css url function', () => {
57
56
58
57
describe ( 'css modules' , ( ) => {
59
58
test ( 'css module compose/from path resolutions' , async ( ) => {
60
- const mockedProjectPath = path . join ( process . cwd ( ) , '/foo/bar/project' )
61
- const { transform, resetMock } = await createCssPluginTransform (
62
- {
63
- [ path . join ( mockedProjectPath , '/css/bar.module.css' ) ] : `\
64
- .bar {
65
- display: block;
66
- background: #f0f;
67
- }` ,
68
- } ,
69
- {
70
- configFile : false ,
71
- resolve : {
72
- alias : [
73
- {
74
- find : '@' ,
75
- replacement : mockedProjectPath ,
76
- } ,
77
- ] ,
78
- } ,
59
+ const { transform } = await createCssPluginTransform ( {
60
+ configFile : false ,
61
+ resolve : {
62
+ alias : [
63
+ {
64
+ find : '@' ,
65
+ replacement : path . join (
66
+ import . meta. dirname ,
67
+ './fixtures/css-module-compose' ,
68
+ ) ,
69
+ } ,
70
+ ] ,
79
71
} ,
80
- )
72
+ } )
81
73
82
74
const result = await transform (
83
75
`\
@@ -88,22 +80,21 @@ composes: bar from '@/css/bar.module.css';
88
80
'/css/foo.module.css' ,
89
81
)
90
82
91
- expect ( result . code ) . toBe (
92
- `\
93
- ._bar_1csqm_1 {
94
- display: block;
95
- background: #f0f;
96
- }
97
- ._foo_86148_1 {
98
- position: fixed;
99
- }` ,
83
+ expect ( result . code ) . toMatchInlineSnapshot (
84
+ `
85
+ "._bar_1b4ow_1 {
86
+ display: block;
87
+ background: #f0f;
88
+ }
89
+ ._foo_86148_1 {
90
+ position: fixed;
91
+ }"
92
+ ` ,
100
93
)
101
-
102
- resetMock ( )
103
94
} )
104
95
105
96
test ( 'custom generateScopedName' , async ( ) => {
106
- const { transform, resetMock } = await createCssPluginTransform ( undefined , {
97
+ const { transform } = await createCssPluginTransform ( {
107
98
configFile : false ,
108
99
css : {
109
100
modules : {
@@ -118,7 +109,6 @@ position: fixed;
118
109
const result1 = await transform ( css , '/foo.module.css' ) // server
119
110
const result2 = await transform ( css , '/foo.module.css?direct' ) // client
120
111
expect ( result1 . code ) . toBe ( result2 . code )
121
- resetMock ( )
122
112
} )
123
113
} )
124
114
@@ -212,10 +202,7 @@ describe('hoist @ rules', () => {
212
202
} )
213
203
} )
214
204
215
- async function createCssPluginTransform (
216
- files ?: Record < string , string > ,
217
- inlineConfig : InlineConfig = { } ,
218
- ) {
205
+ async function createCssPluginTransform ( inlineConfig : InlineConfig = { } ) {
219
206
const config = await resolveConfig ( inlineConfig , 'serve' )
220
207
const environment = new PartialEnvironment ( 'client' , config )
221
208
@@ -224,13 +211,6 @@ async function createCssPluginTransform(
224
211
// @ts -expect-error buildStart is function
225
212
await buildStart . call ( { } )
226
213
227
- const mockFs = vi
228
- . spyOn ( fs , 'readFile' )
229
- // @ts -expect-error vi.spyOn not recognize override `fs.readFile` definition.
230
- . mockImplementationOnce ( ( p , _encoding , callback ) => {
231
- callback ( null , Buffer . from ( files ?. [ p ] ?? '' ) )
232
- } )
233
-
234
214
return {
235
215
async transform ( code : string , id : string ) {
236
216
// @ts -expect-error transform is function
@@ -245,9 +225,6 @@ async function createCssPluginTransform(
245
225
id ,
246
226
)
247
227
} ,
248
- resetMock ( ) {
249
- mockFs . mockReset ( )
250
- } ,
251
228
}
252
229
}
253
230
0 commit comments