Skip to content

Commit

Permalink
test: replace fs mocking in css module compose test (vitejs#18413)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Oct 21, 2024
1 parent 70bb8de commit ddee0ad
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 49 deletions.
75 changes: 26 additions & 49 deletions packages/vite/src/node/__tests__/plugins/css.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fs from 'node:fs'
import path from 'node:path'
import { describe, expect, test, vi } from 'vitest'
import { describe, expect, test } from 'vitest'
import { resolveConfig } from '../../config'
import type { InlineConfig } from '../../config'
import {
Expand Down Expand Up @@ -57,27 +56,20 @@ describe('search css url function', () => {

describe('css modules', () => {
test('css module compose/from path resolutions', async () => {
const mockedProjectPath = path.join(process.cwd(), '/foo/bar/project')
const { transform, resetMock } = await createCssPluginTransform(
{
[path.join(mockedProjectPath, '/css/bar.module.css')]: `\
.bar {
display: block;
background: #f0f;
}`,
},
{
configFile: false,
resolve: {
alias: [
{
find: '@',
replacement: mockedProjectPath,
},
],
},
const { transform } = await createCssPluginTransform({
configFile: false,
resolve: {
alias: [
{
find: '@',
replacement: path.join(
import.meta.dirname,
'./fixtures/css-module-compose',
),
},
],
},
)
})

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

expect(result.code).toBe(
`\
._bar_1csqm_1 {
display: block;
background: #f0f;
}
._foo_86148_1 {
position: fixed;
}`,
expect(result.code).toMatchInlineSnapshot(
`
"._bar_1b4ow_1 {
display: block;
background: #f0f;
}
._foo_86148_1 {
position: fixed;
}"
`,
)

resetMock()
})

test('custom generateScopedName', async () => {
const { transform, resetMock } = await createCssPluginTransform(undefined, {
const { transform } = await createCssPluginTransform({
configFile: false,
css: {
modules: {
Expand All @@ -118,7 +109,6 @@ position: fixed;
const result1 = await transform(css, '/foo.module.css') // server
const result2 = await transform(css, '/foo.module.css?direct') // client
expect(result1.code).toBe(result2.code)
resetMock()
})
})

Expand Down Expand Up @@ -212,10 +202,7 @@ describe('hoist @ rules', () => {
})
})

async function createCssPluginTransform(
files?: Record<string, string>,
inlineConfig: InlineConfig = {},
) {
async function createCssPluginTransform(inlineConfig: InlineConfig = {}) {
const config = await resolveConfig(inlineConfig, 'serve')
const environment = new PartialEnvironment('client', config)

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

const mockFs = vi
.spyOn(fs, 'readFile')
// @ts-expect-error vi.spyOn not recognize override `fs.readFile` definition.
.mockImplementationOnce((p, _encoding, callback) => {
callback(null, Buffer.from(files?.[p] ?? ''))
})

return {
async transform(code: string, id: string) {
// @ts-expect-error transform is function
Expand All @@ -245,9 +225,6 @@ async function createCssPluginTransform(
id,
)
},
resetMock() {
mockFs.mockReset()
},
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.bar {
display: block;
background: #f0f;
}

0 comments on commit ddee0ad

Please sign in to comment.