|
| 1 | +import fs from 'node:fs' |
| 2 | +import os from 'node:os' |
| 3 | +import path from 'node:path' |
| 4 | +import type { ChildProcess } from 'node:child_process' |
| 5 | +import { describe, expect, test } from 'vitest' |
| 6 | +import { createServer } from '../../index' |
| 7 | + |
| 8 | +const getActiveHandles = (): unknown[] => (process as any)._getActiveHandles() |
| 9 | + |
| 10 | +const runningSassWorkers = (): ChildProcess[] => |
| 11 | + getActiveHandles().filter((h): h is ChildProcess => { |
| 12 | + if (!h || (h as object).constructor?.name !== 'ChildProcess') return false |
| 13 | + const cp = h as ChildProcess & { spawnfile?: string } |
| 14 | + return ( |
| 15 | + cp.exitCode == null && |
| 16 | + typeof cp.spawnfile === 'string' && |
| 17 | + cp.spawnfile.includes('sass') |
| 18 | + ) |
| 19 | + }) |
| 20 | + |
| 21 | +describe('css preprocessor worker teardown', () => { |
| 22 | + test('awaits sass-embedded worker disposal on server.close()', async () => { |
| 23 | + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'vite-sass-teardown-')) |
| 24 | + const scssPath = path.join(root, 'a.scss') |
| 25 | + fs.writeFileSync(scssPath, '$c: red;\nbody { color: $c; }\n') |
| 26 | + |
| 27 | + const server = await createServer({ |
| 28 | + root, |
| 29 | + logLevel: 'silent', |
| 30 | + configFile: false, |
| 31 | + server: { ws: false }, |
| 32 | + }) |
| 33 | + await server.listen() |
| 34 | + |
| 35 | + try { |
| 36 | + await server.pluginContainer.transform( |
| 37 | + fs.readFileSync(scssPath, 'utf8'), |
| 38 | + scssPath, |
| 39 | + ) |
| 40 | + } catch { |
| 41 | + // the optimizer can throw ERR_OUTDATED_OPTIMIZED_DEP post-transform; |
| 42 | + // not relevant here — we only need the scss processor to have run. |
| 43 | + } |
| 44 | + |
| 45 | + expect(runningSassWorkers().length).toBeGreaterThan(0) |
| 46 | + |
| 47 | + await server.close() |
| 48 | + |
| 49 | + expect(runningSassWorkers().length).toBe(0) |
| 50 | + }, 30_000) |
| 51 | +}) |
0 commit comments