-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vitest.config.ts
195 lines (187 loc) · 5.67 KB
/
vitest.config.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* @file Vitest Configuration
* @module config/vitest
* @see https://vitest.dev/config/
*/
import { DECORATOR_REGEX } from '@flex-development/decorator-regex'
import pathe from '@flex-development/pathe'
import * as tscu from '@flex-development/tsconfig-utils'
import { ifelse, sift, split, type Nullable } from '@flex-development/tutils'
import ci from 'is-ci'
import ts from 'typescript'
import tsconfigpaths from 'vite-tsconfig-paths'
import {
defineConfig,
type UserConfig,
type UserConfigExport
} from 'vitest/config'
import { BaseSequencer, type WorkspaceSpec } from 'vitest/node'
import tsconfig from './tsconfig.json' assert { type: 'json' }
/**
* Vitest configuration export.
*
* @const {UserConfigExport} config
*/
const config: UserConfigExport = defineConfig((): UserConfig => {
/**
* [`lint-staged`][1] check.
*
* [1]: https://github.com/okonet/lint-staged
*
* @const {boolean} LINT_STAGED
*/
const LINT_STAGED: boolean = !!Number.parseInt(process.env.LINT_STAGED ?? '0')
return {
define: {},
plugins: [
{
enforce: 'pre',
name: 'decorators',
/**
* Transforms source `code` containing decorators.
*
* @param {string} code - Source code
* @param {string} id - Module id of source code
* @return {Nullable<{ code: string }>} Transform result
*/
transform(code: string, id: string): Nullable<{ code: string }> {
// do nothing if source code does not contain decorators
DECORATOR_REGEX.lastIndex = 0
if (!DECORATOR_REGEX.test(code)) return null
/**
* Regular expression used to match constructor parameters.
*
* @see https://regex101.com/r/kTq0JK
*
* @const {RegExp} CONSTRUCTOR_PARAMS_REGEX
*/
const CONSTRUCTOR_PARAMS_REGEX: RegExp =
/(?<=constructor\(\s*)([^\n)].+?)(?=\n? *?\) ?{)/gs
// add ignore comment before constructor parameters
for (const [match] of code.matchAll(CONSTRUCTOR_PARAMS_REGEX)) {
code = code.replace(match, (params: string): string => {
return split(params, '\n').reduce((acc, param) => {
return acc.replace(
param,
param.replace(/(\S)/, '/* c8 ignore next */ $1')
)
}, params)
})
}
return {
code: ts.transpileModule(code, {
compilerOptions: tscu.normalizeCompilerOptions({
...tsconfig.compilerOptions,
inlineSourceMap: true
}),
fileName: id
}).outputText
}
}
},
tsconfigpaths({ projects: [pathe.resolve('tsconfig.json')] })
],
test: {
allowOnly: !ci,
benchmark: {},
chaiConfig: {
includeStack: true,
showDiff: true,
truncateThreshold: 0
},
clearMocks: true,
coverage: {
all: !LINT_STAGED,
clean: true,
cleanOnRerun: true,
exclude: [
'**/__mocks__/',
'**/__tests__/',
'**/interfaces/',
'**/types/',
'**/index.ts',
'src/main.ts'
],
extension: ['.ts'],
include: ['src'],
provider: 'v8',
reporter: [...(ci ? [] : (['html'] as const)), 'lcovonly', 'text'],
reportsDirectory: './coverage',
skipFull: false
},
environment: 'node',
environmentOptions: {},
globalSetup: [],
globals: true,
hookTimeout: 10 * 1000,
include: [
`**/__tests__/*.${LINT_STAGED ? '{spec,spec-d}' : 'spec'}.ts?(x)`
],
mockReset: true,
outputFile: { json: './__tests__/report.json' },
passWithNoTests: true,
reporters: sift([
'json',
'verbose',
ifelse(ci, '', './__tests__/reporters/notifier.ts')
]),
/**
* Stores snapshots next to `file`'s directory.
*
* @param {string} file - Path to test file
* @param {string} extension - Snapshot extension
* @return {string} Custom snapshot path
*/
resolveSnapshotPath(file: string, extension: string): string {
return pathe.resolve(
pathe.resolve(pathe.dirname(pathe.dirname(file)), '__snapshots__'),
pathe.basename(file).replace(/\.spec.tsx?/, '') + extension
)
},
restoreMocks: true,
root: process.cwd(),
sequence: {
sequencer: class Sequencer extends BaseSequencer {
/**
* Determines test file execution order.
*
* @public
* @override
* @async
*
* @param {WorkspaceSpec[]} specs - Workspace spec objects
* @return {Promise<WorkspaceSpec[]>} `files` sorted
*/
public override async sort(
specs: WorkspaceSpec[]
): Promise<WorkspaceSpec[]> {
return (await super.sort(specs)).sort(([, file1], [, file2]) => {
return file1.localeCompare(file2)
})
}
}
},
setupFiles: ['./__tests__/setup/index.ts'],
silent: false,
slowTestThreshold: 5000,
snapshotFormat: {
callToJSON: true,
min: false,
printBasicPrototype: false,
printFunctionName: true
},
testTimeout: 10 * 1000,
typecheck: {
allowJs: false,
checker: 'tsc',
ignoreSourceErrors: false,
include: ['**/__tests__/*.spec-d.ts'],
only: true,
tsconfig: pathe.resolve('tsconfig.typecheck.json')
},
unstubEnvs: true,
unstubGlobals: true
}
}
})
export default config