generated from BiscuitTin/project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvitest.workspace.ts
82 lines (75 loc) · 2.33 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
70
71
72
73
74
75
76
77
78
79
80
81
82
/* eslint-disable @typescript-eslint/no-unsafe-assignment -- We trust the typescript API */
/* eslint-disable @typescript-eslint/only-throw-error -- Ignore this rule */
import fs from 'node:fs'
import path from 'node:path'
import process from 'node:process'
import { type CompilerOptions, findConfigFile, parseJsonConfigFileContent, readConfigFile, sys } from 'typescript'
import { defineWorkspace } from 'vitest/config'
const isTestFileGlobExpr = (expr: string) => expr.includes('.spec.')
const tsconfigPath = findConfigFile(process.cwd(), fs.existsSync)
if (tsconfigPath == null || !tsconfigPath) throw new Error('tsconfig.json not found')
const { config: tsconfig, error } = readConfigFile(
tsconfigPath,
(p) => fs.readFileSync(p, 'utf8'),
)
if (error) throw error
const { projectReferences } = parseJsonConfigFileContent(
tsconfig,
sys,
path.dirname(tsconfigPath),
{},
tsconfigPath,
)
const projects: {
files: string[]
options: CompilerOptions
}[] = []
if (projectReferences) {
for (const { path: p } of projectReferences) {
const { config: tsconfig, error } = readConfigFile(p, (p) => fs.readFileSync(p, 'utf8'))
if (error) throw error
const { options, fileNames, errors } = parseJsonConfigFileContent(
tsconfig,
sys,
path.dirname(p),
{},
p,
)
if (errors.length > 0) {
for (const error of errors) {
console.warn(
`[vitest] ${
typeof error.messageText === 'string'
? error.messageText
: error.messageText.messageText
}`,
)
}
}
if (fileNames.some((name) => isTestFileGlobExpr(name))) {
projects.push({ files: fileNames, options })
}
}
}
export default defineWorkspace(
projects.map(({ files, options }) => ({
extends: './vitest.config.ts',
test: {
name: options.customConditions?.includes('browser') ? 'browser' : 'node',
include: files,
environment: options.customConditions?.includes('browser') ? 'happy-dom' : 'node',
},
ssr: {
target: options.customConditions?.includes('browser') ? 'webworker' : 'node',
},
plugins: [
{
name: 'anonymous:overrideConditions',
config: (config) => {
config.resolve ??= {}
config.resolve.conditions = options.customConditions ?? ['default']
},
},
],
})),
)