diff --git a/.gitignore b/.gitignore index a2e04d27..cf2f7015 100644 --- a/.gitignore +++ b/.gitignore @@ -105,5 +105,4 @@ dist # lib/ -temp/ -coverage/ \ No newline at end of file +temp/ \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index efa37dba..1616cbcc 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,4 @@ { - "vetur.experimental.templateInterpolationService": true + "vetur.experimental.templateInterpolationService": true, + "typescript.tsdk": "node_modules/typescript/lib" } diff --git a/jest.config.js b/jest.config.js index a1ee7e10..c54d4afe 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,13 +1,19 @@ -module.exports = { +// @ts-check + +/** @type {import("@jest/types").Config.InitialOptions} */ +const config = { preset: 'ts-jest', testMatch: ['**/*.spec.[jt]s?(x)'], globalSetup: './scripts/jestGlobalSetup.js', globalTeardown: './scripts/jestGlobalTeardown.js', setupFilesAfterEnv: ['./scripts/jestPerTestSetup.ts'], testTimeout: process.env.CI ? 60000 : 60000, - collectCoverage: true, + collectCoverage: false, collectCoverageFrom: ['packages/*/src/**/*.ts'], + detectOpenHandles: true, transform: { '^.+\\.ts$': 'ts-jest', }, } + +module.exports = config diff --git a/package.json b/package.json index 4efb37e9..893e8afa 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,8 @@ "dev": "pnpm -r --filter ./packages --parallel run dev", "build": "pnpm -r --filter ./packages run build", "lint": "eslint --ext .js,.ts packages/*/src/**", - "test": "jest --runInBand --coverage=false", - "test:coverage": "jest --runInBand", + "test": "jest --runInBand", + "test:coverage": "jest --runInBand --coverage=true", "changelog": "pnpm -r --filter ./packages run changelog", "test:watch": "jest -w", "type-check": "pnpm -r --parallel --filter ./packages exec -- tsc --noEmit -p tsconfig.build.json", diff --git a/packages/vite-plugin-checker/__tests__/e2e/Sandbox/Sandbox.ts b/packages/vite-plugin-checker/__tests__/e2e/Sandbox/Sandbox.ts index f88038b3..437b4235 100644 --- a/packages/vite-plugin-checker/__tests__/e2e/Sandbox/Sandbox.ts +++ b/packages/vite-plugin-checker/__tests__/e2e/Sandbox/Sandbox.ts @@ -35,11 +35,7 @@ export async function viteBuild({ } } -export async function postTest() { - try { - // await fs.remove(tempDir) - } catch (e) {} -} +export function postTest() {} export async function startServer(isBuild: boolean) { // start dev server diff --git a/packages/vite-plugin-checker/__tests__/e2e/build.spec.ts b/packages/vite-plugin-checker/__tests__/e2e/build.spec.ts index e40138ef..63580d9d 100644 --- a/packages/vite-plugin-checker/__tests__/e2e/build.spec.ts +++ b/packages/vite-plugin-checker/__tests__/e2e/build.spec.ts @@ -4,6 +4,7 @@ import os from 'os' import CheckerPlugin from '../../lib/main' import { VlsChecker } from '../../../checker-vls/lib/main' import { MockSandbox } from './MockSandbox/MockSandbox' +import { sleep, WORKER_CLEAN_TIMEOUT } from './testUtils' // ref https://github.com/facebook/jest/issues/936#issuecomment-613220940 jest.mock('child_process', () => { @@ -34,8 +35,9 @@ describe('build', () => { jest.clearAllMocks() }) - afterAll(() => { + afterAll(async () => { jest.restoreAllMocks() + await sleep(WORKER_CLEAN_TIMEOUT) }) it('typescript', () => { diff --git a/packages/vite-plugin-checker/__tests__/e2e/testUtils.ts b/packages/vite-plugin-checker/__tests__/e2e/testUtils.ts index 5826038e..d31ce7e9 100644 --- a/packages/vite-plugin-checker/__tests__/e2e/testUtils.ts +++ b/packages/vite-plugin-checker/__tests__/e2e/testUtils.ts @@ -6,9 +6,9 @@ export function slash(p: string): string { return p.replace(/\\/g, '/') } +export const WORKER_CLEAN_TIMEOUT = process.env.CI ? 6000 : 3000 export const testPath = expect.getState().testPath export const testName = slash(testPath).match(/playground\/([\w-]+)\//)?.[1] -assert(testName, `should detect testName, but got null`) export const testDir = path.resolve(process.env.JEST_ROOT_DIR!, `./temp/${testName}`) export function editFile( @@ -28,3 +28,11 @@ export function expectStdoutNotContains(str: string, unexpectedErrorMsg: string) stdout: expect(str).not.toContain(unexpectedErrorMsg), }) } + +export async function sleep(millSeconds: number, callback?: Function) { + return new Promise((resolve) => { + setTimeout(() => { + resolve() + }, millSeconds) + }).then(() => callback?.()) +} diff --git a/packages/vite-plugin-checker/src/worker.ts b/packages/vite-plugin-checker/src/worker.ts index f0432b52..418d09a1 100644 --- a/packages/vite-plugin-checker/src/worker.ts +++ b/packages/vite-plugin-checker/src/worker.ts @@ -1,5 +1,6 @@ import { ConfigEnv } from 'vite' import { parentPort, Worker, workerData } from 'worker_threads' +import invariant from 'tiny-invariant' import { ACTION_TYPES } from './types' @@ -30,7 +31,9 @@ export function createScript({ buildBin, serverChecker, }: WorkerScriptOptions): Script { + // let _env: ConfigEnv | undefined type CheckerConfig = T & SharedConfig + let _env: ConfigEnv return { mainScript: () => { // initialized in main thread @@ -38,7 +41,9 @@ export function createScript({ checkerConfig: CheckerConfig, env: ConfigEnv ): ConfigureServeChecker => { + _env = env const isBuild = env.command === 'build' + const worker = new Worker(absFilename, { workerData: { env, checkerConfig }, }) @@ -73,7 +78,6 @@ export function createScript({ if (!parentPort) throw Error('should have parentPort as file runs in worker thread') const isBuild = workerData.env.command === 'build' // only run bin command and do not listen message in build mode - if (isBuild) return const port = parentPort.on( 'message', @@ -81,6 +85,7 @@ export function createScript({ switch (action.type) { case ACTION_TYPES.config: { const checkerConfig: T = workerData.checkerConfig + invariant(_env, 'env should be initialized in mainScript, but got undefined') diagnostic = serverChecker.createDiagnostic(checkerConfig) diagnostic.config(action.payload) break @@ -96,6 +101,10 @@ export function createScript({ } } ) + + if (isBuild) { + port.unref() + } }, } } diff --git a/playground/react-ts/__tests__/build.spec.ts b/playground/react-ts/__tests__/build.spec.ts index 40de13f2..bd86aa69 100644 --- a/playground/react-ts/__tests__/build.spec.ts +++ b/playground/react-ts/__tests__/build.spec.ts @@ -3,13 +3,20 @@ import { postTest, viteBuild, } from '../../../packages/vite-plugin-checker/__tests__/e2e/Sandbox/Sandbox' -import { editFile, testDir } from '../../../packages/vite-plugin-checker/__tests__/e2e/testUtils' +import { + editFile, + testDir, + sleep, + WORKER_CLEAN_TIMEOUT, +} from '../../../packages/vite-plugin-checker/__tests__/e2e/testUtils' beforeAll(async () => { await preTest() }) -afterAll(postTest) +afterAll(async () => { + await sleep(WORKER_CLEAN_TIMEOUT) +}) describe('typescript', () => { // describe('dev', () => { diff --git a/playground/vue2-vls/__tests__/build.spec.ts b/playground/vue2-vls/__tests__/build.spec.ts index e650df05..e201d13b 100644 --- a/playground/vue2-vls/__tests__/build.spec.ts +++ b/playground/vue2-vls/__tests__/build.spec.ts @@ -3,13 +3,20 @@ import { postTest, viteBuild, } from '../../../packages/vite-plugin-checker/__tests__/e2e/Sandbox/Sandbox' -import { testDir, editFile } from '../../../packages/vite-plugin-checker/__tests__/e2e/testUtils' +import { + testDir, + editFile, + sleep, + WORKER_CLEAN_TIMEOUT, +} from '../../../packages/vite-plugin-checker/__tests__/e2e/testUtils' beforeAll(async () => { await preTest() }) -afterAll(postTest) +afterAll(async () => { + await sleep(WORKER_CLEAN_TIMEOUT) +}) describe('vue2-vls', () => { // describe('dev', () => { diff --git a/playground/vue3-vue-tsc/__tests__/build.spec.ts b/playground/vue3-vue-tsc/__tests__/build.spec.ts index 88871e26..e5d48439 100644 --- a/playground/vue3-vue-tsc/__tests__/build.spec.ts +++ b/playground/vue3-vue-tsc/__tests__/build.spec.ts @@ -1,15 +1,22 @@ import { - preTest, postTest, + preTest, viteBuild, } from '../../../packages/vite-plugin-checker/__tests__/e2e/Sandbox/Sandbox' -import { testDir, editFile } from '../../../packages/vite-plugin-checker/__tests__/e2e/testUtils' +import { + editFile, + sleep, + testDir, + WORKER_CLEAN_TIMEOUT, +} from '../../../packages/vite-plugin-checker/__tests__/e2e/testUtils' beforeAll(async () => { await preTest() }) -afterAll(postTest) +afterAll(async () => { + await sleep(WORKER_CLEAN_TIMEOUT) +}) describe('vue3-vue-tsc', () => { // describe('dev', () => {