Skip to content

Commit

Permalink
fix(watch): run test files when added to filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Apr 13, 2023
1 parent baf902a commit ec3b520
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/vitest/src/node/core.ts
Expand Up @@ -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])
}
Expand Down
25 changes: 24 additions & 1 deletion 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'
Expand All @@ -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}
Expand All @@ -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 () => {
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit ec3b520

Please sign in to comment.