-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.config.ts
More file actions
160 lines (148 loc) · 5.46 KB
/
Copy pathvitest.config.ts
File metadata and controls
160 lines (148 loc) · 5.46 KB
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
/// <reference types="vitest" />
import { getViteConfig } from 'astro/config'
import { config as loadEnv } from 'dotenv'
import { resolve } from 'path'
import { createLogger, type LogOptions } from 'vite'
import { getSiteUrl } from './src/lib/config'
loadEnv({ path: resolve(process.cwd(), '.env.development'), override: false, quiet: true } as never)
/**
* Needed to be able to skip running integrations in astro.config.ts
* during unit tests
*/
process.env['VITEST'] = 'true'
const shouldShowVitestLogs = Boolean(process.env['VITEST_SHOW_LOGS'])
const stripAnsi = (value: string): string => value
const shouldSuppressEarlyVitestOutput = (value: string): boolean => {
const cleanValue = stripAnsi(value)
return (
/\[astro-icon\]\s*Loaded icons/i.test(cleanValue) ||
/Loaded icons from/i.test(cleanValue) ||
/Local icons changed, reloading/i.test(cleanValue) ||
// Vite SSR module runner teardown noise: the transport between the test
// worker and Vite closes before astro:db / db/seed.ts finish evaluating.
// Harmless — tests have already passed at this point.
/transport was disconnected/i.test(cleanValue)
)
}
const wrapWriteWithEarlyVitestFilter = (write: typeof process.stdout.write): typeof process.stdout.write => {
return ((chunk: unknown, encoding?: unknown, cb?: unknown) => {
try {
const bufferEncoding: BufferEncoding =
typeof encoding === 'string' && Buffer.isEncoding(encoding) ? encoding : 'utf8'
const text =
typeof chunk === 'string'
? chunk
: Buffer.isBuffer(chunk)
? chunk.toString(bufferEncoding)
: String(chunk)
if (text && shouldSuppressEarlyVitestOutput(text)) {
if (typeof cb === 'function') cb()
return true
}
} catch {
// If something goes sideways, never block writes.
}
return (write as unknown as (_value: unknown, _enc?: unknown, _callback?: unknown) => boolean)(
chunk as never,
encoding as never,
cb as never
)
}) as typeof process.stdout.write
}
if (!shouldShowVitestLogs) {
process.stdout.write = wrapWriteWithEarlyVitestFilter(process.stdout.write.bind(process.stdout))
process.stderr.write = wrapWriteWithEarlyVitestFilter(process.stderr.write.bind(process.stderr))
}
const viteLogger = createLogger('error', { allowClearScreen: false })
export default getViteConfig({
logLevel: 'error',
customLogger: {
...viteLogger,
info: (message: string, options?: LogOptions) => {
if (/\[astro-icon\] Loaded icons/i.test(message) || /Loaded icons from/i.test(message)) return
viteLogger.info(message, options)
},
error: (message: string, options?: LogOptions) => {
if (/transport was disconnected/i.test(message)) return
viteLogger.error(message, options)
},
},
define: {
'import.meta.env.SITE': JSON.stringify(getSiteUrl()),
},
resolve: {
alias: {
'@assets': resolve(__dirname, './src/assets'),
'@cache/*': resolve(__dirname, './cache'),
'@components': resolve(__dirname, './src/components'),
'@content': resolve(__dirname, './src/content'),
'@data': resolve(__dirname, './src/data'),
'@layouts': resolve(__dirname, './src/layouts'),
'@lib': resolve(__dirname, './src/lib'),
'@pages': resolve(__dirname, './src/pages'),
'@styles': resolve(__dirname, './src/styles'),
'@test': resolve(__dirname, './test'),
'astro:transitions/client': resolve(__dirname, './test/__mocks__/astro:transitions/client.ts'),
'focus-trap': resolve(__dirname, './test/__mocks__/focus-trap.ts'),
},
},
test: {
mode: 'development',
include: [
'src/**/*.spec.ts?(x)',
'scripts/**/*.spec.ts',
'api/**/*.spec.ts',
'test/unit/**/*.spec.ts',
'test/eslint/__tests__/**/*.spec.ts',
'test/e2e/helpers/__tests__/**/*.spec.ts',
'.github/actions/**/__tests__/**/*.spec.ts',
],
environmentMatchGlobs: [
['src/components/**', 'node'],
['src/**', 'happy-dom'],
['src/lib/**', 'node'],
['scripts/**', 'node'],
['src/pages/api/**', 'node'],
['test/unit/**', 'node'],
['test/eslint/__tests__/**', 'node'],
['test/e2e/helpers/__tests__/**', 'node'],
['.github/actions/**', 'node'],
],
env: {
DEV: 'true',
MODE: 'development',
NODE_ENV: 'development',
PROD: 'false',
VITEST: 'true',
},
// Use GitHub Actions reporter when running in CI so tests produce
// actionable annotations in the Actions UI. Vitest 1.3.0+ includes
// a built-in 'github-actions' reporter, so we rely on that here.
reporters: process.env['GITHUB_ACTIONS'] ? ['default', 'github-actions'] :
'default',
testTimeout: 30 * 1000,
setupFiles: ['./vitest.setup.ts'],
coverage: {
provider: 'v8',
reporter: ['text', 'json-summary', 'json', 'html', 'lcov'],
reportsDirectory: './coverage',
include: ['src/**/*.{ts,tsx,astro}', 'scripts/**/*.ts'],
exclude: [
'src/**/*.spec.ts',
'scripts/**/*.spec.ts',
'src/**/__tests__/**',
'scripts/**/__tests__/**',
'src/@types/**',
'@types/**',
'**/*.d.ts',
],
all: true,
clean: true,
},
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any, {
// Astro's getViteConfig path expects an inline server config object even when
// the project config omits it and relies on defaults.
server: {},
})