-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvitest.workspace.ts
69 lines (62 loc) · 1.9 KB
/
vitest.workspace.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import fs from 'node:fs'
import path from 'node:path'
import process from 'node:process'
import ts from 'typescript'
import { defineWorkspace } from 'vitest/config'
const isTestFileGlobExpr = (expr: string) => expr.includes('.spec.')
const tsconfigPath = ts.findConfigFile(process.cwd(), fs.existsSync)
if (!tsconfigPath) throw new Error('tsconfig.json not found')
// eslint-disable-next-line ts/no-unsafe-assignment
const { config: tsconfig, error } = ts.readConfigFile(tsconfigPath, p => fs.readFileSync(p, 'utf-8'))
if (error) throw error
const { projectReferences } = ts.parseJsonConfigFileContent(
tsconfig,
ts.sys,
path.dirname(tsconfigPath),
{},
tsconfigPath
)
const projects: {
files: string[]
options: ts.CompilerOptions
}[] = []
projectReferences?.forEach(({ path: p }) => {
// eslint-disable-next-line ts/no-unsafe-assignment
const { config: tsconfig, error } = ts.readConfigFile(p, p => fs.readFileSync(p, 'utf-8'))
if (error) throw error
const { options, fileNames, errors } = ts.parseJsonConfigFileContent(tsconfig, ts.sys, path.dirname(p), {}, p)
if (errors.length > 0) {
errors.forEach(e =>
console.warn(`[vitest] ${typeof e.messageText === 'string' ? e.messageText : e.messageText.messageText}`)
)
}
if (fileNames.filter(isTestFileGlobExpr).length > 0) {
projects.push({ files: fileNames, options })
}
})
export default defineWorkspace(projects.map(({
files,
options
}) => ({
extends: './vitest.config.ts',
test: { include: files },
ssr: {
target: options?.customConditions?.includes('browser')
? 'webworker'
: 'node'
},
plugins: [
{
name: 'anonymous:overrideConditions',
config: c => {
if (!c.resolve) {
c.resolve = {}
}
if (!c.resolve.conditions) {
c.resolve.conditions = []
}
c.resolve.conditions = options?.customConditions ?? ['default']
}
}
]
})))