Skip to content

Commit ddee0ad

Browse files
authored
test: replace fs mocking in css module compose test (#18413)
1 parent 70bb8de commit ddee0ad

File tree

2 files changed

+30
-49
lines changed

2 files changed

+30
-49
lines changed

packages/vite/src/node/__tests__/plugins/css.spec.ts

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import fs from 'node:fs'
21
import path from 'node:path'
3-
import { describe, expect, test, vi } from 'vitest'
2+
import { describe, expect, test } from 'vitest'
43
import { resolveConfig } from '../../config'
54
import type { InlineConfig } from '../../config'
65
import {
@@ -57,27 +56,20 @@ describe('search css url function', () => {
5756

5857
describe('css modules', () => {
5958
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+
],
7971
},
80-
)
72+
})
8173

8274
const result = await transform(
8375
`\
@@ -88,22 +80,21 @@ composes: bar from '@/css/bar.module.css';
8880
'/css/foo.module.css',
8981
)
9082

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+
`,
10093
)
101-
102-
resetMock()
10394
})
10495

10596
test('custom generateScopedName', async () => {
106-
const { transform, resetMock } = await createCssPluginTransform(undefined, {
97+
const { transform } = await createCssPluginTransform({
10798
configFile: false,
10899
css: {
109100
modules: {
@@ -118,7 +109,6 @@ position: fixed;
118109
const result1 = await transform(css, '/foo.module.css') // server
119110
const result2 = await transform(css, '/foo.module.css?direct') // client
120111
expect(result1.code).toBe(result2.code)
121-
resetMock()
122112
})
123113
})
124114

@@ -212,10 +202,7 @@ describe('hoist @ rules', () => {
212202
})
213203
})
214204

215-
async function createCssPluginTransform(
216-
files?: Record<string, string>,
217-
inlineConfig: InlineConfig = {},
218-
) {
205+
async function createCssPluginTransform(inlineConfig: InlineConfig = {}) {
219206
const config = await resolveConfig(inlineConfig, 'serve')
220207
const environment = new PartialEnvironment('client', config)
221208

@@ -224,13 +211,6 @@ async function createCssPluginTransform(
224211
// @ts-expect-error buildStart is function
225212
await buildStart.call({})
226213

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-
234214
return {
235215
async transform(code: string, id: string) {
236216
// @ts-expect-error transform is function
@@ -245,9 +225,6 @@ async function createCssPluginTransform(
245225
id,
246226
)
247227
},
248-
resetMock() {
249-
mockFs.mockReset()
250-
},
251228
}
252229
}
253230

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.bar {
2+
display: block;
3+
background: #f0f;
4+
}

0 commit comments

Comments
 (0)