Skip to content

Commit b0609a7

Browse files
authored
test: properly close cli watcher in test (unocss#2323)
1 parent 89f897e commit b0609a7

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ jobs:
4141
os: [ubuntu-latest]
4242
node_version: [14, 16, 18]
4343
include:
44-
# - os: macos-latest
45-
# node_version: 16
44+
- os: macos-latest
45+
node_version: 16
4646
- os: windows-latest
4747
node_version: 16
4848
fail-fast: false

packages/cli/src/index.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { applyTransformers } from '../../shared-integration/src/transformers'
1212
import { PrettyError, handleError } from './errors'
1313
import { defaultConfig } from './config'
1414
import type { CliOptions, ResolvedCliOptions } from './types'
15+
import { getWatcher } from './watcher'
1516

1617
const name = 'unocss'
1718

@@ -58,17 +59,9 @@ export async function build(_options: CliOptions) {
5859
const startWatcher = async () => {
5960
if (!options.watch)
6061
return
61-
62-
const { watch } = await import('chokidar')
6362
const { patterns } = options
64-
const ignored = ['**/{.git,node_modules}/**']
6563

66-
const watcher = watch(patterns, {
67-
ignoreInitial: true,
68-
ignorePermissionErrors: true,
69-
ignored,
70-
cwd,
71-
})
64+
const watcher = await getWatcher(options)
7265

7366
if (configSources.length)
7467
watcher.add(configSources)

packages/cli/src/watcher.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { FSWatcher } from 'chokidar'
2+
import type { CliOptions } from './types'
3+
4+
let watcher: FSWatcher
5+
6+
export const getWatcher = async (options?: CliOptions) => {
7+
// test case entry without options
8+
if (watcher && !options)
9+
return watcher
10+
11+
const { watch } = await import('chokidar')
12+
const ignored = ['**/{.git,node_modules}/**']
13+
// cli may create multiple watchers
14+
const newWatcher = watch(options?.patterns as string[], {
15+
ignoreInitial: true,
16+
ignorePermissionErrors: true,
17+
ignored,
18+
cwd: options?.cwd || process.cwd(),
19+
})
20+
watcher = newWatcher
21+
return newWatcher
22+
}

test/cli.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { resolve } from 'node:path'
22
import fs from 'fs-extra'
33
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
44
import { startCli } from '../packages/cli/src/cli-start'
5+
import { getWatcher } from '../packages/cli/src/watcher'
56

67
export const tempDir = resolve('.temp')
78
export const cli = resolve(__dirname, '../packages/cli/src/cli.ts')
@@ -67,13 +68,14 @@ export default defineConfig({
6768
})
6869
`)
6970
for (let i = 100; i >= 0; i--) {
70-
await sleep(1000)
71+
await sleep(500)
7172
const outputChanged = await readFile(testDir as string)
7273
if (i === 0 || outputChanged.includes('.bg-foo')) {
7374
expect(outputChanged).toContain('.bg-foo{background-color:blue;}')
7475
break
7576
}
7677
}
78+
(await getWatcher()).close()
7779
})
7880

7981
it('supports variantGroup transformer', async () => {

0 commit comments

Comments
 (0)