From 6e96e98b7950a7df8aef136a19d01644621bf20b Mon Sep 17 00:00:00 2001 From: bluwy Date: Fri, 15 Sep 2023 20:32:19 +0800 Subject: [PATCH] refactor: use more stable test --- .../__tests__/fixtures/watched/index.js | 1 - .../src/node/server/__tests__/watcher.spec.ts | 58 +++---------------- 2 files changed, 8 insertions(+), 51 deletions(-) delete mode 100644 packages/vite/src/node/server/__tests__/fixtures/watched/index.js diff --git a/packages/vite/src/node/server/__tests__/fixtures/watched/index.js b/packages/vite/src/node/server/__tests__/fixtures/watched/index.js deleted file mode 100644 index b474d09ca388ca..00000000000000 --- a/packages/vite/src/node/server/__tests__/fixtures/watched/index.js +++ /dev/null @@ -1 +0,0 @@ -export const test = 'initial text' diff --git a/packages/vite/src/node/server/__tests__/watcher.spec.ts b/packages/vite/src/node/server/__tests__/watcher.spec.ts index feace03b528634..90b68fcfd88d0f 100644 --- a/packages/vite/src/node/server/__tests__/watcher.spec.ts +++ b/packages/vite/src/node/server/__tests__/watcher.spec.ts @@ -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((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) }) })