Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: replace fs mocking in css module compose test #18413

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}