Skip to content

Commit

Permalink
fix(watch): don't indicate exit when no matching files
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Nov 28, 2024
1 parent fecc56a commit b82e5ea
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
5 changes: 4 additions & 1 deletion packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,10 @@ export class Vitest {

this.logger.printNoTestFound(filters)

if (!this.config.watch || !(this.config.changed || this.config.related?.length)) {
if (this.config.watch) {
this.report('onFinished', [], [])
}
else if (!this.config.watch || !(this.config.changed || this.config.related?.length)) {
const exitCode = this.config.passWithNoTests ? 0 : 1
process.exitCode = exitCode
throw new FilesNotFoundError(this.mode)
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/node/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,11 @@ export class Logger {
}
else {
if (config.passWithNoTests) {
this.log(`No ${config.mode} files found, exiting with code 0\n`)
this.log(`No ${config.mode} files found${config.watch ? '' : ', exiting with code 0'}`)
}
else {
this.error(
c.red(`\nNo ${config.mode} files found, exiting with code 1`),
c.red(`\nNo ${config.mode} files found${config.watch ? '' : ', exiting with code 1'}`),
)
}
}
Expand Down
36 changes: 36 additions & 0 deletions test/config/test/filename-pattern.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,42 @@ test('match by pattern that also matches current working directory', async () =>
expect(stdout).not.toMatch('test/example.test.ts')
})

test('no matches by file name in run mode {passWithNoTests: false}', async () => {
const { exitCode, stderr } = await runVitest(
{ passWithNoTests: false, root: './fixtures/filters' },
['non-matching-filename-pattern'],
)

expect(exitCode).toBe(1)
expect(stderr).toMatch('No test files found, exiting with code 1')
expect(stderr).toMatch('filter: non-matching-filename-pattern')
})

test('no matches by file name in run mode {passWithNoTests: true}', async () => {
const { exitCode, stdout, stderr } = await runVitest(
{ passWithNoTests: true, root: './fixtures/filters' },
['non-matching-filename-pattern'],
)

expect(exitCode).toBe(0)
expect(stdout).toMatch('No test files found, exiting with code 0')
expect(stderr).toMatch('filter: non-matching-filename-pattern')
})

test('no matches by file name in watch mode', async () => {
const { exitCode, stdout, stderr } = await runVitest(
{ watch: true, root: './fixtures/filters' },
['non-matching-filename-pattern'],
)

expect(exitCode).toBe(0)
expect(stderr).not.toMatch('exiting')

expect(stderr).toMatch('No test files found\n')
expect(stderr).toMatch('filter: non-matching-filename-pattern')
expect(stdout).toMatch('Waiting for file changes...')
})

test.each([
['the parent of CWD', resolve(process.cwd(), '..')],
['the parent of CWD with slash', join(resolve(process.cwd(), '..'), '/')],
Expand Down
15 changes: 13 additions & 2 deletions test/config/test/pass-empty-files.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { expect, it } from 'vitest'
import { runVitest } from '../../test-utils'

it('vitest doesnt fail when running empty files', async () => {
const { exitCode } = await runVitest({
it('vitest doesnt fail when running empty files {passWithNoTests: true}', async () => {
const { exitCode, stdout } = await runVitest({
root: './fixtures/pass-empty-files',
passWithNoTests: true,
})
expect(exitCode).toBe(0)
expect(stdout).toMatch('no tests')
})

it('vitest fails when running empty files {passWithNoTests: false}', async () => {
const { exitCode, stderr } = await runVitest({
root: './fixtures/pass-empty-files',
passWithNoTests: false,
})

expect(exitCode).toBe(1)
expect(stderr).toMatch('Error: No test suite found in file')
})

0 comments on commit b82e5ea

Please sign in to comment.