From 5f1f2c3cbec257440415f1783ecb387cc341792c Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 21 Oct 2024 17:49:56 +0900 Subject: [PATCH] test: refactor out fs mock --- .../src/node/__tests__/plugins/css.spec.ts | 75 +++++++------------ .../css-module-compose/css/bar.module.css | 4 + 2 files changed, 30 insertions(+), 49 deletions(-) create mode 100644 packages/vite/src/node/__tests__/plugins/fixtures/css-module-compose/css/bar.module.css diff --git a/packages/vite/src/node/__tests__/plugins/css.spec.ts b/packages/vite/src/node/__tests__/plugins/css.spec.ts index 891690e58b6f51..5fdbbf45f05fa5 100644 --- a/packages/vite/src/node/__tests__/plugins/css.spec.ts +++ b/packages/vite/src/node/__tests__/plugins/css.spec.ts @@ -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 { @@ -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( `\ @@ -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: { @@ -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() }) }) @@ -212,10 +202,7 @@ describe('hoist @ rules', () => { }) }) -async function createCssPluginTransform( - files?: Record, - inlineConfig: InlineConfig = {}, -) { +async function createCssPluginTransform(inlineConfig: InlineConfig = {}) { const config = await resolveConfig(inlineConfig, 'serve') const environment = new PartialEnvironment('client', config) @@ -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 @@ -245,9 +225,6 @@ async function createCssPluginTransform( id, ) }, - resetMock() { - mockFs.mockReset() - }, } } diff --git a/packages/vite/src/node/__tests__/plugins/fixtures/css-module-compose/css/bar.module.css b/packages/vite/src/node/__tests__/plugins/fixtures/css-module-compose/css/bar.module.css new file mode 100644 index 00000000000000..fa163ddd5179cc --- /dev/null +++ b/packages/vite/src/node/__tests__/plugins/fixtures/css-module-compose/css/bar.module.css @@ -0,0 +1,4 @@ +.bar { + display: block; + background: #f0f; +}