Skip to content

Commit

Permalink
test: add runInlineTests
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Nov 7, 2024
1 parent 01164c8 commit bffa6fb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 34 deletions.
28 changes: 27 additions & 1 deletion test/test-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,24 @@ export function resolvePath(baseUrl: string, path: string) {

export function useFS(root: string, structure: Record<string, string | ViteUserConfig | WorkspaceProjectConfiguration[]>) {
const files = new Set<string>()
const hasConfig = Object.keys(structure).some(file => file.includes('.config.'))
if (!hasConfig) {
structure['./vitest.config.js'] = {}
}
for (const file in structure) {
const filepath = resolve(root, file)
files.add(filepath)
const content = typeof structure[file] === 'string'
? structure[file]
: `export default ${JSON.stringify(structure[file])}`
createFile(resolve(root, filepath), String(content))
fs.mkdirSync(dirname(filepath), { recursive: true })
fs.writeFileSync(filepath, content, 'utf-8')
}
onTestFinished(() => {
if (process.env.VITEST_FS_CLEANUP !== 'false') {
fs.rmSync(root, { recursive: true, force: true })
}
})
return {
editFile: (file: string, callback: (content: string) => string) => {
const filepath = resolve(root, file)
Expand All @@ -267,3 +277,19 @@ export function useFS(root: string, structure: Record<string, string | ViteUserC
},
}
}

export async function runInlineTests(
structure: Record<string, string | ViteUserConfig | WorkspaceProjectConfiguration[]>,
config?: UserConfig,
) {
const root = resolve(process.cwd(), `vitest-test-${crypto.randomUUID()}`)
const fs = useFS(root, structure)
const vitest = await runVitest({
root,
...config,
})
return {
fs,
...vitest,
}
}
53 changes: 20 additions & 33 deletions test/watch/test/config-watching.test.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,58 @@
import { resolve } from 'pathe'
import { expect, test } from 'vitest'
import { runVitest, useFS } from '../../test-utils'
import { runInlineTests } from '../../test-utils'

const ts = String.raw

test('reruns tests when configs change', async () => {
const root = resolve(import.meta.dirname, '../fixtures/config-watching')
const { editFile } = useFS(root, {
'./vitest.workspace.ts': [
const { fs, vitest } = await runInlineTests({
'vitest.workspace.ts': [
'./project-1',
'./project-2',
],
'./vitest.config.ts': {},
'./project-1/vitest.config.ts': {},
'./project-1/basic.test.ts': ts`
'vitest.config.ts': {},
'project-1/vitest.config.ts': {},
'project-1/basic.test.ts': ts`
import { test } from 'vitest'
test('basic test 1', () => {})
`,
'./project-2/vitest.config.ts': {},
'./project-2/basic.test.ts': ts`
'project-2/vitest.config.ts': {},
'project-2/basic.test.ts': ts`
import { test } from 'vitest'
test('basic test 2', () => {})
`,
})

const { vitest } = await runVitest({
root,
watch: true,
})
}, { watch: true })

await vitest.waitForStdout('Waiting for file changes')
vitest.resetOutput()

// editing the project config should trigger a restart
editFile('./project-1/vitest.config.ts', c => `\n${c}`)
fs.editFile('./project-1/vitest.config.ts', c => `\n${c}`)

await vitest.waitForStdout('Restarting due to config changes...')
await vitest.waitForStdout('Waiting for file changes')
vitest.resetOutput()

// editing the root config should trigger a restart
editFile('./vitest.config.ts', c => `\n${c}`)
fs.editFile('./vitest.config.ts', c => `\n${c}`)

await vitest.waitForStdout('Restarting due to config changes...')
await vitest.waitForStdout('Waiting for file changes')
vitest.resetOutput()

// editing the workspace config should trigger a restart
editFile('./vitest.workspace.ts', c => `\n${c}`)
fs.editFile('./vitest.workspace.ts', c => `\n${c}`)

await vitest.waitForStdout('Restarting due to config changes...')
await vitest.waitForStdout('Waiting for file changes')
})

test('rerun stops the previous browser server and restarts multiple times without port mismatch', async () => {
const root = resolve(import.meta.dirname, '../fixtures/browser-config-watching')
const { editFile } = useFS(root, {
'./vitest.workspace.ts': [
const { fs, vitest } = await runInlineTests({
'vitest.workspace.ts': [
'./project-1',
],
'./vitest.config.ts': {},
'./project-1/vitest.config.ts': {
'vitest.config.ts': {},
'project-1/vitest.config.ts': {
test: {
browser: {
enabled: true,
Expand All @@ -70,22 +62,17 @@ test('rerun stops the previous browser server and restarts multiple times withou
},
},
},
'./project-1/basic.test.ts': ts`
'project-1/basic.test.ts': ts`
import { test } from 'vitest'
test('basic test 1', () => {})
`,
})

const { vitest } = await runVitest({
root,
watch: true,
})
}, { watch: true })

await vitest.waitForStdout('Waiting for file changes')
vitest.resetOutput()

// editing the project config the first time restarts the browser server
editFile('./project-1/vitest.config.ts', c => `\n${c}`)
fs.editFile('./project-1/vitest.config.ts', c => `\n${c}`)

await vitest.waitForStdout('Restarting due to config changes...')
await vitest.waitForStdout('Waiting for file changes')
Expand All @@ -95,7 +82,7 @@ test('rerun stops the previous browser server and restarts multiple times withou
vitest.resetOutput()

// editing the project the second time also restarts the server
editFile('./project-1/vitest.config.ts', c => `\n${c}`)
fs.editFile('./project-1/vitest.config.ts', c => `\n${c}`)

await vitest.waitForStdout('Restarting due to config changes...')
await vitest.waitForStdout('Waiting for file changes')
Expand Down

0 comments on commit bffa6fb

Please sign in to comment.