Skip to content

Commit

Permalink
refactor: use more stable test
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Sep 15, 2023
1 parent 1139d04 commit 6e96e98
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 51 deletions.

This file was deleted.

58 changes: 8 additions & 50 deletions packages/vite/src/node/server/__tests__/watcher.spec.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,24 @@
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { writeFileSync } from 'node:fs'
import { afterEach, describe, expect, it } from 'vitest'
import { describe, expect, it } from 'vitest'
import { createServer } from '../index'

const __dirname = dirname(fileURLToPath(import.meta.url))
const stubGetWatchedCode = /getWatched\(\) \{.+?return \{\};.+?\}/s

afterEach(() => {
writeFileSync(
resolve(__dirname, 'fixtures/watched/index.js'),
"export const test = 'initial text'\n",
)
})

// watcher is unstable on windows
describe.skipIf(process.platform === 'win32')('watcher configuration', () => {
it("when watcher is disabled, editing files doesn't trigger watcher", async () => {
describe('watcher configuration', () => {
it('when watcher is disabled, return noop watcher', async () => {
const server = await createServer({
server: {
watch: null,
},
})
server.watcher.on('change', () => {
expect.unreachable()
})

server.watcher.add(resolve(__dirname, 'fixtures/watched/index.js'))
writeFileSync(
resolve(__dirname, 'fixtures/watched/index.js'),
'export const test = "new text"',
)

// make sure watcher doesn't trigger
await new Promise((resolve) => setTimeout(resolve, 500))
expect(server.watcher.getWatched.toString()).toMatch(stubGetWatchedCode)
})

it('when watcher is not disable, editing files triggers watcher', async () => {
expect.assertions(1)
it('when watcher is not disabled, return chokidar watcher', async () => {
const server = await createServer({
server: {
// "ready" event might not be triggered on macos otherwise
watch:
process.platform === 'darwin'
? {
useFsEvents: false,
usePolling: false,
}
: {},
watch: {},
},
})
const filename = resolve(__dirname, 'fixtures/watched/index.js')

return new Promise<void>((resolve) => {
server.watcher.on('change', (e) => {
expect(e).toMatch('/fixtures/watched/index.js')
resolve()
})

server.watcher.on('ready', () => {
server.watcher.add(filename)
writeFileSync(filename, 'export const test = "new text"')
})
})
expect(server.watcher.getWatched.toString()).not.toMatch(stubGetWatchedCode)
})
})

0 comments on commit 6e96e98

Please sign in to comment.