From f16fae582b218c8dce634d618a17cd653f784ae9 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Thu, 13 Jun 2024 17:10:18 +0800 Subject: [PATCH] test: disable isolate for unit test (#17448) --- .../fixtures/watcher/config-deps/foo.js | 0 .../fixtures/watcher/custom-env/.env | 0 .../fixtures/watcher/custom-public/foo.txt | 0 .../watcher/nested-root/vite.config.js | 7 ++ .../__tests__/fixtures/watcher/package.json | 4 + .../src/node/server/__tests__/watcher.spec.ts | 85 ++++++------------- .../src/node/ssr/runtime/__tests__/utils.ts | 4 +- vitest.config.ts | 1 + 8 files changed, 41 insertions(+), 60 deletions(-) create mode 100644 packages/vite/src/node/server/__tests__/fixtures/watcher/config-deps/foo.js create mode 100644 packages/vite/src/node/server/__tests__/fixtures/watcher/custom-env/.env create mode 100644 packages/vite/src/node/server/__tests__/fixtures/watcher/custom-public/foo.txt create mode 100644 packages/vite/src/node/server/__tests__/fixtures/watcher/nested-root/vite.config.js create mode 100644 packages/vite/src/node/server/__tests__/fixtures/watcher/package.json diff --git a/packages/vite/src/node/server/__tests__/fixtures/watcher/config-deps/foo.js b/packages/vite/src/node/server/__tests__/fixtures/watcher/config-deps/foo.js new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/packages/vite/src/node/server/__tests__/fixtures/watcher/custom-env/.env b/packages/vite/src/node/server/__tests__/fixtures/watcher/custom-env/.env new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/packages/vite/src/node/server/__tests__/fixtures/watcher/custom-public/foo.txt b/packages/vite/src/node/server/__tests__/fixtures/watcher/custom-public/foo.txt new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/packages/vite/src/node/server/__tests__/fixtures/watcher/nested-root/vite.config.js b/packages/vite/src/node/server/__tests__/fixtures/watcher/nested-root/vite.config.js new file mode 100644 index 00000000000000..e4a957d8539ba4 --- /dev/null +++ b/packages/vite/src/node/server/__tests__/fixtures/watcher/nested-root/vite.config.js @@ -0,0 +1,7 @@ +import { defineConfig } from 'vite' +import '../config-deps/foo.js' + +export default defineConfig({ + envDir: '../custom-env', + publicDir: '../custom-public', +}) diff --git a/packages/vite/src/node/server/__tests__/fixtures/watcher/package.json b/packages/vite/src/node/server/__tests__/fixtures/watcher/package.json new file mode 100644 index 00000000000000..e986b24bbae58b --- /dev/null +++ b/packages/vite/src/node/server/__tests__/fixtures/watcher/package.json @@ -0,0 +1,4 @@ +{ + "private": true, + "type": "module" +} diff --git a/packages/vite/src/node/server/__tests__/watcher.spec.ts b/packages/vite/src/node/server/__tests__/watcher.spec.ts index df0e8c0641d0d7..3c4a009f6f178a 100644 --- a/packages/vite/src/node/server/__tests__/watcher.spec.ts +++ b/packages/vite/src/node/server/__tests__/watcher.spec.ts @@ -1,49 +1,22 @@ import { resolve } from 'node:path' -import { - type MockInstance, - afterEach, - beforeEach, - describe, - expect, - it, - vi, -} from 'vitest' -import chokidar from 'chokidar' -import { createServer } from '../index' +import { fileURLToPath } from 'node:url' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { type ViteDevServer, createServer } from '../index' const stubGetWatchedCode = /getWatched\(\) \{.+?return \{\};.+?\}/s -let watchSpy: MockInstance< - Parameters, - ReturnType -> - -vi.mock('../../config', async () => { - const config: typeof import('../../config') = - await vi.importActual('../../config') - const resolveConfig = config.resolveConfig - vi.spyOn(config, 'resolveConfig').mockImplementation(async (...args) => { - const resolved: Awaited> = - await resolveConfig.call(config, ...args) - resolved.configFileDependencies.push( - resolve('fake/config/dependency.js').replace(/\\/g, '/'), - ) - return resolved - }) - return config -}) - describe('watcher configuration', () => { - beforeEach(() => { - watchSpy = vi.spyOn(chokidar, 'watch') - }) + let server: ViteDevServer | undefined - afterEach(() => { - watchSpy.mockRestore() + afterEach(async () => { + if (server) { + await server.close() + server = undefined + } }) it('when watcher is disabled, return noop watcher', async () => { - const server = await createServer({ + server = await createServer({ server: { watch: null, }, @@ -52,7 +25,7 @@ describe('watcher configuration', () => { }) it('when watcher is not disabled, return chokidar watcher', async () => { - const server = await createServer({ + server = await createServer({ server: { watch: {}, }, @@ -61,25 +34,23 @@ describe('watcher configuration', () => { }) it('should watch the root directory, config file dependencies, dotenv files, and the public directory', async () => { - await createServer({ - server: { - watch: {}, - }, - publicDir: '__test_public__', - }) - expect(watchSpy).toHaveBeenLastCalledWith( - expect.arrayContaining( - [ - process.cwd(), - resolve('fake/config/dependency.js'), - resolve('.env'), - resolve('.env.local'), - resolve('.env.development'), - resolve('.env.development.local'), - resolve('__test_public__'), - ].map((file) => file.replace(/\\/g, '/')), - ), - expect.anything(), + const root = fileURLToPath( + new URL('./fixtures/watcher/nested-root', import.meta.url), ) + server = await createServer({ root }) + await new Promise((resolve) => server!.watcher.once('ready', resolve)) + // Perform retries here as chokidar may still not be completely watching all directories + // after the `ready` event + await vi.waitFor(() => { + const watchedDirs = Object.keys(server!.watcher.getWatched()) + expect(watchedDirs).toEqual( + expect.arrayContaining([ + root, + resolve(root, '../config-deps'), + resolve(root, '../custom-env'), + resolve(root, '../custom-public'), + ]), + ) + }) }) }) diff --git a/packages/vite/src/node/ssr/runtime/__tests__/utils.ts b/packages/vite/src/node/ssr/runtime/__tests__/utils.ts index 7e14bb986e828a..5d8c06d4bc3a46 100644 --- a/packages/vite/src/node/ssr/runtime/__tests__/utils.ts +++ b/packages/vite/src/node/ssr/runtime/__tests__/utils.ts @@ -38,9 +38,7 @@ export async function createViteRuntimeTester( server: { middlewareMode: true, watch: null, - hmr: { - port: 9609, - }, + ws: false, }, ssr: { external: ['@vitejs/cjs-external', '@vitejs/esm-external'], diff --git a/vitest.config.ts b/vitest.config.ts index 2802969bc155c2..4167b96a90a280 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -14,6 +14,7 @@ export default defineConfig({ './playground-temp/**/*.*', ], testTimeout: 20000, + isolate: false, }, esbuild: { target: 'node18',