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: disable isolate for unit test #17448

Merged
merged 4 commits into from
Jun 13, 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
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vite'
import '../config-deps/foo.js'

export default defineConfig({
envDir: '../custom-env',
publicDir: '../custom-public',
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"private": true,
"type": "module"
}
85 changes: 28 additions & 57 deletions packages/vite/src/node/server/__tests__/watcher.spec.ts
Original file line number Diff line number Diff line change
@@ -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<typeof chokidar.watch>,
ReturnType<typeof chokidar.watch>
>

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<ReturnType<typeof resolveConfig>> =
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,
},
Expand All @@ -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: {},
},
Expand All @@ -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'),
]),
)
})
})
})
4 changes: 1 addition & 3 deletions packages/vite/src/node/ssr/runtime/__tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default defineConfig({
'./playground-temp/**/*.*',
],
testTimeout: 20000,
isolate: false,
},
esbuild: {
target: 'node18',
Expand Down