Skip to content

Commit

Permalink
test(filters): re-use runVitestCli from test-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed May 23, 2023
1 parent 518dd16 commit e8ef758
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 153 deletions.
6 changes: 0 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions test/watch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
},
"devDependencies": {
"@vitest/browser": "workspace:*",
"execa": "^7.0.0",
"strip-ansi": "^7.0.1",
"vite": "latest",
"vitest": "workspace:*",
"webdriverio": "latest"
Expand Down
49 changes: 25 additions & 24 deletions test/watch/test/file-watching.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync, rmSync, writeFileSync } from 'node:fs'
import { afterEach, describe, test } from 'vitest'

import { startWatchMode } from './utils'
import { runVitestCli } from '../../test-utils'

const sourceFile = 'fixtures/math.ts'
const sourceFileContent = readFileSync(sourceFile, 'utf-8')
Expand All @@ -12,6 +12,7 @@ const testFileContent = readFileSync(testFile, 'utf-8')
const configFile = 'fixtures/vitest.config.ts'
const configFileContent = readFileSync(configFile, 'utf-8')

const cliArgs = ['--root', 'fixtures', '--watch']
const cleanups: (() => void)[] = []

function editFile(fileContent: string) {
Expand All @@ -29,46 +30,46 @@ afterEach(() => {
})

test('editing source file triggers re-run', async () => {
const vitest = await startWatchMode()
const vitest = await runVitestCli(...cliArgs)

writeFileSync(sourceFile, editFile(sourceFileContent), 'utf8')

await vitest.waitForOutput('New code running')
await vitest.waitForOutput('RERUN ../math.ts')
await vitest.waitForOutput('1 passed')
await vitest.waitForStdout('New code running')
await vitest.waitForStdout('RERUN ../math.ts')
await vitest.waitForStdout('1 passed')
})

test('editing test file triggers re-run', async () => {
const vitest = await startWatchMode()
const vitest = await runVitestCli(...cliArgs)

writeFileSync(testFile, editFile(testFileContent), 'utf8')

await vitest.waitForOutput('New code running')
await vitest.waitForOutput('RERUN ../math.test.ts')
await vitest.waitForOutput('1 passed')
await vitest.waitForStdout('New code running')
await vitest.waitForStdout('RERUN ../math.test.ts')
await vitest.waitForStdout('1 passed')
})

test('editing config file triggers re-run', async () => {
const vitest = await startWatchMode()
const vitest = await runVitestCli(...cliArgs)

writeFileSync(configFile, editFile(configFileContent), 'utf8')

await vitest.waitForOutput('New code running')
await vitest.waitForOutput('Restarting due to config changes')
await vitest.waitForOutput('2 passed')
await vitest.waitForStdout('New code running')
await vitest.waitForStdout('Restarting due to config changes')
await vitest.waitForStdout('2 passed')
})

test('editing config file reloads new changes', async () => {
const vitest = await startWatchMode()
const vitest = await runVitestCli(...cliArgs)

writeFileSync(configFile, configFileContent.replace('reporters: \'verbose\'', 'reporters: \'tap\''), 'utf8')

await vitest.waitForOutput('TAP version')
await vitest.waitForOutput('ok 2')
await vitest.waitForStdout('TAP version')
await vitest.waitForStdout('ok 2')
})

test('adding a new test file triggers re-run', async () => {
const vitest = await startWatchMode()
const vitest = await runVitestCli(...cliArgs)

const testFile = 'fixtures/new-dynamic.test.ts'
const testFileContent = `
Expand All @@ -82,20 +83,20 @@ test("dynamic test case", () => {
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')
await vitest.waitForStdout('Running added dynamic test')
await vitest.waitForStdout('RERUN ../new-dynamic.test.ts')
await vitest.waitForStdout('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')
const vitest = await runVitestCli(...cliArgs, '--browser.enabled', '--browser.headless', '--browser.name=chrome')

writeFileSync(sourceFile, editFile(sourceFileContent), 'utf8')

await vitest.waitForOutput('New code running')
await vitest.waitForOutput('RERUN ../math.ts')
await vitest.waitForOutput('1 passed')
await vitest.waitForStdout('New code running')
await vitest.waitForStdout('RERUN ../math.ts')
await vitest.waitForStdout('1 passed')

vitest.write('q')
})
Expand Down
39 changes: 20 additions & 19 deletions test/watch/test/stdin.test.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,60 @@
import { rmSync, writeFileSync } from 'node:fs'
import { afterEach, expect, test } from 'vitest'

import { startWatchMode } from './utils'
import { runVitestCli } from '../../test-utils'

const cliArgs = ['--root', 'fixtures', '--watch']
const cleanups: (() => void)[] = []

afterEach(() => {
cleanups.splice(0).forEach(fn => fn())
})

test('quit watch mode', async () => {
const vitest = await startWatchMode()
const vitest = await runVitestCli(...cliArgs)

vitest.write('q')

await vitest.isDone
})

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

vitest.write('r')

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

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

vitest.write('p')

await vitest.waitForOutput('Input filename pattern')
await vitest.waitForStdout('Input filename pattern')

vitest.write('math\n')

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

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

vitest.write('t')

await vitest.waitForOutput('Input test name pattern')
await vitest.waitForStdout('Input test name pattern')

vitest.write('sum\n')

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

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

const testPath = 'fixtures/cancel.test.ts'
const testCase = `// Dynamic test case
Expand All @@ -78,13 +79,13 @@ test('2 - test that is cancelled', async () => {
writeFileSync(testPath, testCase, 'utf8')

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

// Test hooks should still be called
await vitest.waitForOutput('CANCELLED')
await vitest.waitForOutput('[cancel-test]: afterAll')
await vitest.waitForOutput('[cancel-test]: afterEach')
await vitest.waitForStdout('CANCELLED')
await vitest.waitForStdout('[cancel-test]: afterAll')
await vitest.waitForStdout('[cancel-test]: afterEach')

expect(vitest.output).not.include('[cancel-test]: should not run')
expect(vitest.stdout).not.include('[cancel-test]: should not run')
})
12 changes: 6 additions & 6 deletions test/watch/test/stdout.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync, writeFileSync } from 'node:fs'
import { afterEach, test } from 'vitest'

import { startWatchMode } from './utils'
import { runVitestCli } from '../../test-utils'

const testFile = 'fixtures/math.test.ts'
const testFileContent = readFileSync(testFile, 'utf-8')
Expand All @@ -11,7 +11,7 @@ afterEach(() => {
})

test('console.log is visible on test re-run', async () => {
const vitest = await startWatchMode()
const vitest = await runVitestCli('--root', 'fixtures', '--watch')
const testCase = `
test('test with logging', () => {
console.log('First')
Expand All @@ -23,8 +23,8 @@ test('test with logging', () => {

writeFileSync(testFile, `${testFileContent}${testCase}`, 'utf8')

await vitest.waitForOutput('stdout | math.test.ts > test with logging')
await vitest.waitForOutput('First')
await vitest.waitForOutput('Second')
await vitest.waitForOutput('Third')
await vitest.waitForStdout('stdout | math.test.ts > test with logging')
await vitest.waitForStdout('First')
await vitest.waitForStdout('Second')
await vitest.waitForStdout('Third')
})
68 changes: 0 additions & 68 deletions test/watch/test/utils.ts

This file was deleted.

Loading

0 comments on commit e8ef758

Please sign in to comment.