From 361a17c0a5455df392cd66c229245b5cf015c666 Mon Sep 17 00:00:00 2001 From: AriPerkkio Date: Thu, 13 Apr 2023 12:34:19 +0300 Subject: [PATCH] fix(watch): run test files when added to filesystem --- packages/vitest/src/node/core.ts | 2 ++ test/watch/test/file-watching.test.ts | 25 ++++++++++++++++++- test/watch/test/workspaces.test.ts | 36 +++++++++++++++++++++++++-- 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index 9383ee21eea3..58c72e141328 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -592,6 +592,8 @@ export class Vitest { id = slash(id) updateLastChanged(id) if (await this.isTargetFile(id)) { + this.projectsTestFiles.set(id, new Set(this.projects)) + this.changedTests.add(id) this.scheduleRerun([id]) } diff --git a/test/watch/test/file-watching.test.ts b/test/watch/test/file-watching.test.ts index 1d01e0ac2330..d40a97e8aafc 100644 --- a/test/watch/test/file-watching.test.ts +++ b/test/watch/test/file-watching.test.ts @@ -1,4 +1,4 @@ -import { readFileSync, writeFileSync } from 'node:fs' +import { readFileSync, rmSync, writeFileSync } from 'node:fs' import { afterEach, describe, test } from 'vitest' import { startWatchMode } from './utils' @@ -12,6 +12,8 @@ const testFileContent = readFileSync(testFile, 'utf-8') const configFile = 'fixtures/vitest.config.ts' const configFileContent = readFileSync(configFile, 'utf-8') +const cleanups: (() => void)[] = [] + function editFile(fileContent: string) { return `// Modified by file-watching.test.ts ${fileContent} @@ -23,6 +25,7 @@ afterEach(() => { writeFileSync(sourceFile, sourceFileContent, 'utf8') writeFileSync(testFile, testFileContent, 'utf8') writeFileSync(configFile, configFileContent, 'utf8') + cleanups.splice(0).forEach(cleanup => cleanup()) }) test('editing source file triggers re-run', async () => { @@ -64,6 +67,26 @@ test('editing config file reloads new changes', async () => { await vitest.waitForOutput('ok 2') }) +test('adding a new test file triggers re-run', async () => { + const vitest = await startWatchMode() + + const testFile = 'fixtures/new-dynamic.test.ts' + const testFileContent = ` +import { expect, test } from "vitest"; + +test("dynamic test case", () => { + console.log("Running added dynamic test") + expect(true).toBeTruthy() +}) +` + cleanups.push(() => rmSync(testFile)) + writeFileSync(testFile, testFileContent, 'utf-8') + + await vitest.waitForOutput('Running added dynamic test') + await vitest.waitForOutput('RERUN ../new-dynamic.test.ts') + await vitest.waitForOutput('1 passed') +}) + describe('browser', () => { test.runIf((process.platform !== 'win32'))('editing source file triggers re-run', async () => { const vitest = await startWatchMode('--browser.enabled', '--browser.headless', '--browser.name=chrome') diff --git a/test/watch/test/workspaces.test.ts b/test/watch/test/workspaces.test.ts index a9bc61f9f749..8335b227dc0c 100644 --- a/test/watch/test/workspaces.test.ts +++ b/test/watch/test/workspaces.test.ts @@ -1,6 +1,6 @@ import { fileURLToPath } from 'node:url' -import { readFileSync, writeFileSync } from 'node:fs' -import { afterAll, it } from 'vitest' +import { readFileSync, rmSync, writeFileSync } from 'node:fs' +import { afterAll, afterEach, expect, it } from 'vitest' import { dirname, resolve } from 'pathe' import { startWatchMode } from './utils' @@ -8,6 +8,7 @@ const file = fileURLToPath(import.meta.url) const dir = dirname(file) const root = resolve(dir, '..', '..', 'workspaces') const config = resolve(root, 'vitest.config.ts') +const cleanups: (() => void)[] = [] const srcMathFile = resolve(root, 'src', 'math.ts') const specSpace2File = resolve(root, 'space_2', 'test', 'node.spec.ts') @@ -27,6 +28,10 @@ function startVitest() { ) } +afterEach(() => { + cleanups.splice(0).forEach(cleanup => cleanup()) +}) + afterAll(() => { writeFileSync(srcMathFile, srcMathContent, 'utf8') writeFileSync(specSpace2File, specSpace2Content, 'utf8') @@ -65,3 +70,30 @@ it('filters by test name inside a workspace', async () => { await vitest.waitForOutput('Test name pattern: /2 x 2 = 4/') await vitest.waitForOutput('Test Files 1 passed') }) + +it('adding a new test file triggers re-run', async () => { + const vitest = await startVitest() + + const testFile = resolve(root, 'space_2', 'test', 'new-dynamic.test.ts') + const testFileContent = ` +import { expect, test } from "vitest"; + +test("dynamic test case", () => { + console.log("Running added dynamic test") + expect(true).toBeTruthy() +}) +` + cleanups.push(() => rmSync(testFile)) + writeFileSync(testFile, testFileContent, 'utf-8') + + // Wait for tests to end + await vitest.waitForOutput('Waiting for file changes') + + await vitest.waitForOutput('Running added dynamic test') + await vitest.waitForOutput('RERUN space_2/test/new-dynamic.test.ts') + await vitest.waitForOutput('|space_2| test/new-dynamic.test.ts') + + // Test case should not be run by other projects + expect(vitest.output).not.include('|space_1| ../space_2/test/new-dynamic.test.ts') + expect(vitest.output).not.include('|space_3| ../space_2/test/new-dynamic.test.ts') +})