/
testname-pattern.test.ts
50 lines (40 loc) · 1.39 KB
/
testname-pattern.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { resolve } from 'pathe'
import { expect, test } from 'vitest'
import type { File } from 'vitest'
import { startVitest } from 'vitest/node'
test('match by partial pattern', async () => {
const output = await runVitest('example')
expect(output).toMatchInlineSnapshot('"pass: test/example.test.ts"')
})
test('match by full test file name', async () => {
const filename = resolve('./fixtures/test/example.test.ts')
const output = await runVitest(filename)
expect(output).toMatchInlineSnapshot('"pass: test/example.test.ts"')
})
test('match by pattern that also matches current working directory', async () => {
const filter = 'filters'
expect(process.cwd()).toMatch(filter)
const output = await runVitest(filter)
expect(output).toMatchInlineSnapshot('"pass: test/filters.test.ts"')
})
async function runVitest(...cliArgs: string[]) {
let resolve: (value: string) => void
const promise = new Promise<string>((_resolve) => {
resolve = _resolve
})
await startVitest('test', cliArgs, {
root: './fixtures',
watch: false,
reporters: [{
onFinished(files?: File[], errors?: unknown[]) {
if (errors?.length)
resolve(`Error: ${JSON.stringify(errors, null, 2)}`)
if (files)
resolve(files.map(file => `${file.result?.state}: ${file.name}`).join('\n'))
else
resolve('No files')
},
}],
})
return promise
}