test: disable isolate for unit test (#17448) · vitejs/vite@f16fae5 (original) (raw)
1
1
`import { resolve } from 'node:path'
`
2
``
`-
import {
`
3
``
`-
type MockInstance,
`
4
``
`-
afterEach,
`
5
``
`-
beforeEach,
`
6
``
`-
describe,
`
7
``
`-
expect,
`
8
``
`-
it,
`
9
``
`-
vi,
`
10
``
`-
} from 'vitest'
`
11
``
`-
import chokidar from 'chokidar'
`
12
``
`-
import { createServer } from '../index'
`
``
2
`+
import { fileURLToPath } from 'node:url'
`
``
3
`+
import { afterEach, describe, expect, it, vi } from 'vitest'
`
``
4
`+
import { type ViteDevServer, createServer } from '../index'
`
13
5
``
14
6
`const stubGetWatchedCode = /getWatched() {.+?return {};.+?}/s
`
15
7
``
16
``
`-
let watchSpy: MockInstance<
`
17
``
`-
Parameters,
`
18
``
`-
ReturnType
`
19
``
`-
`
20
``
-
21
``
`-
vi.mock('../../config', async () => {
`
22
``
`-
const config: typeof import('../../config') =
`
23
``
`-
await vi.importActual('../../config')
`
24
``
`-
const resolveConfig = config.resolveConfig
`
25
``
`-
vi.spyOn(config, 'resolveConfig').mockImplementation(async (...args) => {
`
26
``
`-
const resolved: Awaited<ReturnType> =
`
27
``
`-
await resolveConfig.call(config, ...args)
`
28
``
`-
resolved.configFileDependencies.push(
`
29
``
`-
resolve('fake/config/dependency.js').replace(/\/g, '/'),
`
30
``
`-
)
`
31
``
`-
return resolved
`
32
``
`-
})
`
33
``
`-
return config
`
34
``
`-
})
`
35
``
-
36
8
`describe('watcher configuration', () => {
`
37
``
`-
beforeEach(() => {
`
38
``
`-
watchSpy = vi.spyOn(chokidar, 'watch')
`
39
``
`-
})
`
``
9
`+
let server: ViteDevServer | undefined
`
40
10
``
41
``
`-
afterEach(() => {
`
42
``
`-
watchSpy.mockRestore()
`
``
11
`+
afterEach(async () => {
`
``
12
`+
if (server) {
`
``
13
`+
await server.close()
`
``
14
`+
server = undefined
`
``
15
`+
}
`
43
16
`})
`
44
17
``
45
18
`it('when watcher is disabled, return noop watcher', async () => {
`
46
``
`-
const server = await createServer({
`
``
19
`+
server = await createServer({
`
47
20
`server: {
`
48
21
`watch: null,
`
49
22
`},
`
`@@ -52,7 +25,7 @@ describe('watcher configuration', () => {
`
52
25
`})
`
53
26
``
54
27
`it('when watcher is not disabled, return chokidar watcher', async () => {
`
55
``
`-
const server = await createServer({
`
``
28
`+
server = await createServer({
`
56
29
`server: {
`
57
30
`watch: {},
`
58
31
`},
`
`@@ -61,25 +34,23 @@ describe('watcher configuration', () => {
`
61
34
`})
`
62
35
``
63
36
`it('should watch the root directory, config file dependencies, dotenv files, and the public directory', async () => {
`
64
``
`-
await createServer({
`
65
``
`-
server: {
`
66
``
`-
watch: {},
`
67
``
`-
},
`
68
``
`-
publicDir: 'test_public',
`
69
``
`-
})
`
70
``
`-
expect(watchSpy).toHaveBeenLastCalledWith(
`
71
``
`-
expect.arrayContaining(
`
72
``
`-
[
`
73
``
`-
process.cwd(),
`
74
``
`-
resolve('fake/config/dependency.js'),
`
75
``
`-
resolve('.env'),
`
76
``
`-
resolve('.env.local'),
`
77
``
`-
resolve('.env.development'),
`
78
``
`-
resolve('.env.development.local'),
`
79
``
`-
resolve('test_public'),
`
80
``
`-
].map((file) => file.replace(/\/g, '/')),
`
81
``
`-
),
`
82
``
`-
expect.anything(),
`
``
37
`+
const root = fileURLToPath(
`
``
38
`+
new URL('./fixtures/watcher/nested-root', import.meta.url),
`
83
39
`)
`
``
40
`+
server = await createServer({ root })
`
``
41
`+
await new Promise((resolve) => server!.watcher.once('ready', resolve))
`
``
42
`+
// Perform retries here as chokidar may still not be completely watching all directories
`
``
43
`` +
// after the ready event
``
``
44
`+
await vi.waitFor(() => {
`
``
45
`+
const watchedDirs = Object.keys(server!.watcher.getWatched())
`
``
46
`+
expect(watchedDirs).toEqual(
`
``
47
`+
expect.arrayContaining([
`
``
48
`+
root,
`
``
49
`+
resolve(root, '../config-deps'),
`
``
50
`+
resolve(root, '../custom-env'),
`
``
51
`+
resolve(root, '../custom-public'),
`
``
52
`+
]),
`
``
53
`+
)
`
``
54
`+
})
`
84
55
`})
`
85
56
`})
`