Skip to content

Commit

Permalink
test: refactor to new test-util API
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Apr 7, 2024
1 parent 7bb90f6 commit 754dc39
Show file tree
Hide file tree
Showing 21 changed files with 131 additions and 155 deletions.
2 changes: 2 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ export default antfu(
'**/bench.json',
'**/fixtures',
'test/core/src/self',
'test/cache/cache/.vitest-base/results.json',
'test/wasm-modules/src/wasm-bindgen-no-cyclic',
'test/workspaces/results.json',
'test/workspaces-browser/results.json',
'test/reporters/fixtures/with-syntax-error.test.js',
'test/network-imports/public/slash@3.0.0.js',
'test/coverage-test/src/transpiled.js',
Expand Down
24 changes: 12 additions & 12 deletions test/config/test/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ const root = resolve(__dirname, '../fixtures/cache')
const project = resolve(__dirname, '../')

test('default', async () => {
const { vitest, stdout, stderr } = await runVitest({
const { ctx, stdout, stderr } = await runVitest({
root,
include: ['*.test.ts'],
})

expect(stdout).toContain('✓ basic.test.ts >')
expect(stderr).toBe('')

const cachePath = vitest!.cache.results.getCachePath()
const cachePath = ctx!.cache.results.getCachePath()
const path = resolve(project, 'node_modules/.vite/vitest/results.json')
expect(cachePath).toMatch(path)
})

test('use cache.dir', async () => {
const { vitest, stdout, stderr } = await runVitest(
const { ctx, stdout, stderr } = await runVitest(
{
root,
include: ['*.test.ts'],
Expand All @@ -33,13 +33,13 @@ test('use cache.dir', async () => {
expect(stdout).toContain('✓ basic.test.ts >')
expect(stderr).toContain('"cache.dir" is deprecated')

const cachePath = vitest!.cache.results.getCachePath()
const cachePath = ctx!.cache.results.getCachePath()
const path = resolve(root, 'node_modules/.vitest-custom/results.json')
expect(cachePath).toMatch(path)
})

test('use cacheDir', async () => {
const { vitest, stdout, stderr } = await runVitest(
const { ctx, stdout, stderr } = await runVitest(
{
root,
include: ['*.test.ts'],
Expand All @@ -52,7 +52,7 @@ test('use cacheDir', async () => {
expect(stdout).toContain('✓ basic.test.ts >')
expect(stderr).toBe('')

const cachePath = vitest!.cache.results.getCachePath()
const cachePath = ctx!.cache.results.getCachePath()
const path = resolve(root, 'node_modules/.vite-custom/vitest/results.json')
expect(cachePath).toMatch(path)
})
Expand All @@ -67,7 +67,7 @@ describe('with optimizer enabled', () => {
}

test('default', async () => {
const { vitest, stdout, stderr } = await runVitest({
const { ctx, stdout, stderr } = await runVitest({
root,
include: ['*.test.ts'],
deps,
Expand All @@ -76,13 +76,13 @@ describe('with optimizer enabled', () => {
expect(stdout).toContain('✓ basic.test.ts >')
expect(stderr).toBe('')

const cachePath = vitest!.cache.results.getCachePath()
const cachePath = ctx!.cache.results.getCachePath()
const path = resolve(project, 'node_modules/.vite/vitest/results.json')
expect(cachePath).toBe(path)
})

test('use cache.dir', async () => {
const { vitest, stdout, stderr } = await runVitest(
const { ctx, stdout, stderr } = await runVitest(
{
root,
include: ['*.test.ts'],
Expand All @@ -96,13 +96,13 @@ describe('with optimizer enabled', () => {
expect(stdout).toContain('✓ basic.test.ts >')
expect(stderr).toContain('"cache.dir" is deprecated')

const cachePath = vitest!.cache.results.getCachePath()
const cachePath = ctx!.cache.results.getCachePath()
const path = resolve(root, 'node_modules/.vitest-custom/results.json')
expect(cachePath).toBe(path)
})

test('use cacheDir', async () => {
const { vitest, stdout, stderr } = await runVitest(
const { ctx, stdout, stderr } = await runVitest(
{
root,
include: ['*.test.ts'],
Expand All @@ -116,7 +116,7 @@ describe('with optimizer enabled', () => {
expect(stdout).toContain('✓ basic.test.ts >')
expect(stderr).toBe('')

const cachePath = vitest!.cache.results.getCachePath()
const cachePath = ctx!.cache.results.getCachePath()
const path = resolve(root, 'node_modules/.vite-custom/vitest/results.json')
expect(cachePath).toBe(path)
})
Expand Down
11 changes: 8 additions & 3 deletions test/config/test/console.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, test } from 'vitest'
import { expect, test, vi } from 'vitest'
import { runVitest } from '../../test-utils'

test('default intercept', async () => {
Expand All @@ -9,10 +9,15 @@ test('default intercept', async () => {
})

test.each(['threads', 'vmThreads'] as const)(`disable intercept pool=%s`, async (pool) => {
const { stderr } = await runVitest({
// `disableConsoleIntercept: true` forwards workers console.error to main thread's stderr
const spy = vi.spyOn(process.stderr, 'write').mockImplementation(() => true)

await runVitest({
root: './fixtures/console',
disableConsoleIntercept: true,
pool,
})
expect(stderr).toBe('__test_console__\n')

const call = spy.mock.lastCall![0]
expect(call.toString()).toBe('__test_console__\n')
})
4 changes: 2 additions & 2 deletions test/config/test/flags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect, it } from 'vitest'
import { runVitest } from '../../test-utils'

it('correctly inherit from the cli', async () => {
const { vitest } = await runVitest({
const { ctx } = await runVitest({
root: 'fixtures/workspace-flags',
logHeapUsage: true,
allowOnly: true,
Expand All @@ -18,7 +18,7 @@ it('correctly inherit from the cli', async () => {
passWithNoTests: true,
bail: 100,
})
const project = vitest!.projects[0]
const project = ctx!.projects[0]
const config = project.getSerializableConfig()
expect(config).toMatchObject({
logHeapUsage: true,
Expand Down
12 changes: 6 additions & 6 deletions test/config/test/shuffle-options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ test.each([
{ files: false, tests: true },
],
)('should use BaseSequencer if shuffle is %o', async (shuffle) => {
const { vitest } = await run({ shuffle })
expect(vitest?.config.sequence.sequencer.name).toBe('BaseSequencer')
const { ctx } = await run({ shuffle })
expect(ctx?.config.sequence.sequencer.name).toBe('BaseSequencer')
})

test.each([
true,
{ files: true, tests: false },
{ files: true, tests: true },
])('should use RandomSequencer if shuffle is %o', async (shuffle) => {
const { vitest } = await run({ shuffle })
expect(vitest?.config.sequence.sequencer.name).toBe('RandomSequencer')
const { ctx } = await run({ shuffle })
expect(ctx?.config.sequence.sequencer.name).toBe('RandomSequencer')
})

test.each([
Expand All @@ -44,6 +44,6 @@ test.each([
{ files: true, tests: false },
{ files: true, tests: true },
])('should always use CustomSequencer if passed', async (shuffle) => {
const { vitest } = await run({ shuffle, sequencer: CustomSequencer })
expect(vitest?.config.sequence.sequencer.name).toBe('CustomSequencer')
const { ctx } = await run({ shuffle, sequencer: CustomSequencer })
expect(ctx?.config.sequence.sequencer.name).toBe('CustomSequencer')
})
2 changes: 1 addition & 1 deletion test/core/test/cli-test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ test('clearScreen', async () => {
clearScreen: viteClearScreen,
}
const vitestConfig = getCLIOptions(vitestClearScreen)
const config = resolveConfig('test', vitestConfig, viteConfig)
const config = resolveConfig('test', vitestConfig, viteConfig, undefined as any)
return config.clearScreen
})
expect(results).toMatchInlineSnapshot(`
Expand Down
4 changes: 2 additions & 2 deletions test/inspect/test/inspect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { runVitestCli } from '../../test-utils'
type Message = Partial<InspectorNotification<any>>

test.skipIf(isWindows)('--inspect-brk stops at test file', async () => {
const vitest = await runVitestCli('--root', 'fixtures', '--inspect-brk', '--no-file-parallelism')
const { vitest, waitForClose } = await runVitestCli('--root', 'fixtures', '--inspect-brk', '--no-file-parallelism')

await vitest.waitForStderr('Debugger listening on ')
const url = vitest.stderr.split('\n')[0].replace('Debugger listening on ', '')
Expand Down Expand Up @@ -36,7 +36,7 @@ test.skipIf(isWindows)('--inspect-brk stops at test file', async () => {
send({ method: 'Debugger.resume' })

await vitest.waitForStdout('Test Files 1 passed (1)')
await vitest.isDone
await waitForClose()
})

async function createChannel(url: string) {
Expand Down
4 changes: 2 additions & 2 deletions test/public-api/tests/runner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ it.each([
const taskUpdate: TaskResultPack[] = []
const finishedFiles: File[] = []
const collectedFiles: File[] = []
const { vitest, stdout, stderr } = await runVitest({
const { ctx, stdout, stderr } = await runVitest({
root: resolve(__dirname, '..', 'fixtures'),
include: ['**/*.spec.ts'],
reporters: [
Expand Down Expand Up @@ -50,7 +50,7 @@ it.each([
expect(taskUpdate).toHaveLength(4)
expect(finishedFiles).toHaveLength(1)

const files = vitest?.state.getFiles() || []
const files = ctx?.state.getFiles() || []
expect(files).toHaveLength(1)

expect(taskUpdate).toContainEqual(
Expand Down
16 changes: 7 additions & 9 deletions test/reporters/tests/default.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, test } from 'vitest'
import { runVitest, runVitestCli } from '../../test-utils'
import { runVitest } from '../../test-utils'

describe('default reporter', async () => {
test('normal', async () => {
Expand Down Expand Up @@ -28,14 +28,12 @@ describe('default reporter', async () => {
})

test('rerun should undo', async () => {
const vitest = await runVitestCli(
'--root',
'fixtures/default',
'--watch',
'-t',
'passed',
)
vitest.resetOutput()
const { vitest } = await runVitest({
root: 'fixtures/default',
watch: true,
testNamePattern: 'passed',
reporters: 'none',
})

// one file
vitest.write('p')
Expand Down
8 changes: 4 additions & 4 deletions test/run/pool-custom-fixtures/pool/custom-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ export default (ctx: Vitest): ProcessPool => {
return {
name: 'custom',
async runTests(specs) {
console.warn('[pool] printing:', options.print)
console.warn('[pool] array option', options.array)
ctx.logger.console.warn('[pool] printing:', options.print)
ctx.logger.console.warn('[pool] array option', options.array)
for await (const [project, file] of specs) {
ctx.state.clearFiles(project)
const methods = createMethodsRPC(project)
console.warn('[pool] running tests for', project.getName(), 'in', normalize(file).toLowerCase().replace(normalize(process.cwd()).toLowerCase(), ''))
ctx.logger.console.warn('[pool] running tests for', project.getName(), 'in', normalize(file).toLowerCase().replace(normalize(process.cwd()).toLowerCase(), ''))
const path = relative(project.config.root, file)
const taskFile: File = {
id: `${path}${project.getName()}`,
Expand Down Expand Up @@ -47,7 +47,7 @@ export default (ctx: Vitest): ProcessPool => {
}
},
close() {
console.warn('[pool] custom pool is closed!')
ctx.logger.console.warn('[pool] custom pool is closed!')
},
}
}
9 changes: 4 additions & 5 deletions test/run/test/tty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { expect, test } from 'vitest'
import { runVitestCli } from '../../test-utils'

test('run mode does not get stuck when TTY', async () => {
const vitest = await runVitestCli('--root', 'fixtures')
await vitest.isDone
const { vitest } = await runVitestCli('--root', 'fixtures')

expect(vitest.stdout).toContain('✓ example.test.ts')
expect(vitest.stdout).toContain('✓ math.test.ts')
expect(vitest.stdout).toContain('2 passed')
await vitest.waitForStdout('✓ example.test.ts')
await vitest.waitForStdout('✓ math.test.ts')
await vitest.waitForStdout('2 passed')

// Regression #3642
expect(vitest.stderr).not.toContain('close timed out')
Expand Down
1 change: 1 addition & 0 deletions test/test-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export async function runVitest(config: UserConfig, cliFilters: string[] = [], m

afterEach(async () => {
await ctx?.close()
await ctx?.closingPromise
process.exit = exit
})
}
Expand Down

0 comments on commit 754dc39

Please sign in to comment.