Skip to content

Commit

Permalink
test: add tests for --standalone mode
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Apr 22, 2024
1 parent add70c4 commit 1f3326e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 56 deletions.
18 changes: 14 additions & 4 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { CancelReason, File } from '@vitest/runner'
import { ViteNodeServer } from 'vite-node/server'
import type { defineWorkspace } from 'vitest/config'
import type { ArgumentsType, CoverageProvider, OnServerRestartHandler, Reporter, ResolvedConfig, UserConfig, UserWorkspaceConfig, VitestRunMode } from '../types'
import { hasFailed, noop, slash, toArray, wildcardPatternToRegExp } from '../utils'
import { getTasks, hasFailed, noop, slash, toArray, wildcardPatternToRegExp } from '../utils'
import { getCoverageProvider } from '../integrations/coverage'
import { CONFIG_NAMES, configFiles, workspacesFiles as workspaceFiles } from '../constants'
import { rootDir } from '../paths'
Expand Down Expand Up @@ -619,14 +619,24 @@ export class Vitest {
if (pattern === '')
this.filenamePattern = undefined

this.configOverride.testNamePattern = pattern ? new RegExp(pattern) : undefined
const testNamePattern = pattern ? new RegExp(pattern) : undefined
this.configOverride.testNamePattern = testNamePattern
// filter only test files that have tests matching the pattern
if (testNamePattern) {
files = files.filter((filepath) => {
const files = this.state.getFiles([filepath])
return !files.length || files.some((file) => {
const tasks = getTasks(file)
return !tasks.length || tasks.some(task => testNamePattern.test(task.name))
})
})
}
await this.rerunFiles(files, trigger)
}

async changeFilenamePattern(pattern: string) {
async changeFilenamePattern(pattern: string, files: string[] = this.state.getFilepaths()) {
this.filenamePattern = pattern

const files = this.state.getFilepaths()
const trigger = this.filenamePattern ? 'change filename pattern' : 'reset filename pattern'

await this.rerunFiles(files, trigger)
Expand Down
15 changes: 12 additions & 3 deletions packages/vitest/src/node/stdin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import readline from 'node:readline'
import c from 'picocolors'
import prompt from 'prompts'
import { relative } from 'pathe'
import { relative, resolve } from 'pathe'
import { getTests, isWindows, stdout } from '../utils'
import { toArray } from '../utils/base'
import type { Vitest } from './core'
Expand Down Expand Up @@ -112,7 +112,13 @@ export function registerConsoleShortcuts(ctx: Vitest) {
})

on()
await ctx.changeNamePattern(filter?.trim() || '', undefined, 'change pattern')
const files = ctx.state.getFilepaths()
// if running in standalone mode, Vitest instance doesn't know about any test file
const cliFiles = ctx.config.standalone && !files.length
? (await ctx.globTestFiles()).map(([_, file]) => file)
: undefined

await ctx.changeNamePattern(filter?.trim() || '', cliFiles, 'change pattern')
}

async function inputProjectName() {
Expand Down Expand Up @@ -143,7 +149,10 @@ export function registerConsoleShortcuts(ctx: Vitest) {

latestFilename = filter?.trim() || ''

await ctx.changeFilenamePattern(latestFilename)
await ctx.changeFilenamePattern(
latestFilename,
watchFilter.getLastResults().map(i => resolve(ctx.config.root, i)),
)
}

let rl: readline.Interface | undefined
Expand Down
4 changes: 4 additions & 0 deletions packages/vitest/src/node/watch-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,8 @@ export class WatchFilter {
private cancel() {
stdout().write(`${ESC}J`) // erase down
}

public getLastResults() {
return this.results
}
}
106 changes: 57 additions & 49 deletions test/watch/test/stdin.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { rmSync, writeFileSync } from 'node:fs'
import { afterEach, expect, test } from 'vitest'
import { afterEach, describe, expect, test } from 'vitest'

import * as testUtils from '../../test-utils'

Expand All @@ -21,63 +21,61 @@ afterEach(() => {
if (process.env.GITHUB_ACTIONS)
test.only('skip tests on CI', () => {})

test('quit watch mode', async () => {
const vitest = await runVitestCli(...cliArgs)
describe.each([['--standalone'], []])('watch mode %s', (...args) => {
const allCliArgs = [...cliArgs, ...args]

vitest.write('q')
test('quit watch mode', async () => {
const vitest = await runVitestCli(...allCliArgs)

await vitest.isDone
})
vitest.write('q')

test('rerun current pattern tests', async () => {
const vitest = await runVitestCli(...cliArgs, '-t', 'sum')
await vitest.isDone
})

vitest.write('r')
test('filter by filename', async () => {
const vitest = await runVitestCli(...allCliArgs)

await vitest.waitForStdout('Test name pattern: /sum/')
await vitest.waitForStdout('1 passed')
})
vitest.write('p')

test('filter by filename', async () => {
const vitest = await runVitestCli(...cliArgs)
await vitest.waitForStdout('Input filename pattern')

vitest.write('p')
vitest.write('math')

await vitest.waitForStdout('Input filename pattern')
await vitest.waitForStdout('Pattern matches 1 result')
await vitest.waitForStdout('› math.test.ts')

vitest.write('math')
vitest.write('\n')

await vitest.waitForStdout('Pattern matches 1 result')
await vitest.waitForStdout('› math.test.ts')
await vitest.waitForStdout('Filename pattern: math')
await vitest.waitForStdout('1 passed')
})

vitest.write('\n')
test('filter by test name', async () => {
const vitest = await runVitestCli(...allCliArgs)

await vitest.waitForStdout('Filename pattern: math')
await vitest.waitForStdout('1 passed')
})
vitest.write('t')

test('filter by test name', async () => {
const vitest = await runVitestCli(...cliArgs)
await vitest.waitForStdout('Input test name pattern')

vitest.write('t')
vitest.write('sum')
if (args[0] === '--standalone')
await vitest.waitForStdout('Pattern matches no results')
else
await vitest.waitForStdout('Pattern matches 1 result')

await vitest.waitForStdout('Input test name pattern')
await vitest.waitForStdout('› sum')

vitest.write('sum')
await vitest.waitForStdout('Pattern matches 1 result')
await vitest.waitForStdout('› sum')
vitest.write('\n')

vitest.write('\n')

await vitest.waitForStdout('Test name pattern: /sum/')
await vitest.waitForStdout('1 passed')
})
await vitest.waitForStdout('Test name pattern: /sum/')
await vitest.waitForStdout('1 passed')
})

test('cancel test run', async () => {
const vitest = await runVitestCli(...cliArgs)
test('cancel test run', async () => {
const vitest = await runVitestCli(...allCliArgs)

const testPath = 'fixtures/cancel.test.ts'
const testCase = `// Dynamic test case
const testPath = 'fixtures/cancel.test.ts'
const testCase = `// Dynamic test case
import { afterAll, afterEach, test } from 'vitest'
// These should be called even when test is cancelled
Expand All @@ -95,17 +93,27 @@ test('2 - test that is cancelled', async () => {
})
`

cleanups.push(() => rmSync(testPath))
writeFileSync(testPath, testCase, 'utf8')
cleanups.push(() => rmSync(testPath))
writeFileSync(testPath, testCase, 'utf8')

// Test case is running, cancel it
await vitest.waitForStdout('[cancel-test]: test')
vitest.write('c')

// Test case is running, cancel it
await vitest.waitForStdout('[cancel-test]: test')
vitest.write('c')
// Test hooks should still be called
await vitest.waitForStdout('CANCELLED')
await vitest.waitForStdout('[cancel-test]: afterAll')
await vitest.waitForStdout('[cancel-test]: afterEach')

// Test hooks should still be called
await vitest.waitForStdout('CANCELLED')
await vitest.waitForStdout('[cancel-test]: afterAll')
await vitest.waitForStdout('[cancel-test]: afterEach')
expect(vitest.stdout).not.include('[cancel-test]: should not run')
})
})

expect(vitest.stdout).not.include('[cancel-test]: should not run')
test('rerun current pattern tests', async () => {
const vitest = await runVitestCli(...cliArgs, '-t', 'sum')

vitest.write('r')

await vitest.waitForStdout('Test name pattern: /sum/')
await vitest.waitForStdout('1 passed')
})

0 comments on commit 1f3326e

Please sign in to comment.