-
-
Notifications
You must be signed in to change notification settings - Fork 9.9k
Expand file tree
/
Copy pathvitest.config.ts
More file actions
131 lines (117 loc) · 4.26 KB
/
vitest.config.ts
File metadata and controls
131 lines (117 loc) · 4.26 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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {fileURLToPath} from 'url';
import {defineConfig, type ViteUserConfig} from 'vitest/config';
import react from '@vitejs/plugin-react';
import {transformWithOxc} from 'vite';
// Force UTC so snapshots involving dates are stable across local & CI runs.
process.env.TZ = 'UTC';
const rootDir = fileURLToPath(new URL('.', import.meta.url));
const ignorePatterns = [
'**/node_modules/**',
'**/__fixtures__/**',
'**/__mocks__/**',
'**/testUtils.ts',
// Built outputs — we test sources, not the compiled `lib/`.
'**/packages/docusaurus/lib/**',
'**/packages/docusaurus-logger/lib/**',
'**/packages/docusaurus-utils/lib/**',
'**/packages/docusaurus-utils-common/lib/**',
'**/packages/docusaurus-utils-validation/lib/**',
'**/packages/docusaurus-plugin-content-blog/lib/**',
'**/packages/docusaurus-plugin-content-docs/lib/**',
'**/packages/docusaurus-plugin-content-pages/lib/**',
'**/packages/docusaurus-theme-classic/lib/**',
'**/packages/docusaurus-theme-common/lib/**',
'**/packages/docusaurus-migrate/lib/**',
'**/test/**',
'**/argos/**',
];
type AliasOptions = NonNullable<ViteUserConfig['resolve']>['alias'];
const resolveAliases: AliasOptions = [
// Resolve `@docusaurus/<exportName>` to `docusaurus-core` source files
{
find: /^@docusaurus\/(BrowserOnly|ComponentCreator|constants|ExecutionEnvironment|Head|Interpolate|isInternalUrl|Link|Noop|renderRoutes|router|Translate|use.*)$/,
replacement: '@docusaurus/core/src/client/exports/$1',
},
// Stub `@generated/*` modules for tests
{find: /^@generated\/.*/, replacement: `${rootDir}test/emptyModule.ts`},
// Resolve `@theme/*` aliases to theme-classic components
// This won't always be true, but good enough for now
{
find: /^@theme\/(.*)$/,
replacement: '@docusaurus/theme-classic/src/theme/$1',
},
// Resolve `@site/*` aliases against our own website
// Because our own site can have tests involving that alias
{find: /^@site\/(.*)$/, replacement: `${rootDir}website/$1`},
// Resolving content plugins client exports to src instead of lib
// This way, tests work without having to rebuild those packages
{
find: /^@docusaurus\/plugin-content-(docs|blog|pages)\/client$/,
replacement: '@docusaurus/plugin-content-$1/src/client/index.ts',
},
];
// Apparently Vitest can't interpret .js files as JSX?
// and we have many files like that in theme lib/theme output dir
// Not sure why we need this to ensure .js files are parsed as JSX by Oxc
// This must be configurable??? But can't find how
function docusaurusJsAsJsx() {
return {
name: 'docusaurus-js-as-jsx',
enforce: 'pre' as const,
async transform(code: string, id: string) {
const cleanId = id.split('?')[0];
if (
cleanId.endsWith('.js') &&
cleanId.includes('/packages/docusaurus-')
) {
return transformWithOxc(code, cleanId, {
lang: 'jsx',
});
}
return null;
},
};
}
export default defineConfig({
plugins: [docusaurusJsAsJsx(), react()],
resolve: {
alias: resolveAliases,
},
test: {
setupFiles: ['test/setup.ts'],
environment: 'node',
environmentOptions: {
jsdom: {
url: 'https://docusaurus.io/',
},
},
// Default 5s timeout often fails on Windows runners.
// See https://github.com/facebook/docusaurus/pull/8259
testTimeout: 25000,
exclude: ignorePatterns,
snapshotFormat: {
// Vitest defaults are fine
},
snapshotSerializers: [
// TOP: applied last
// Capture Error.cause in snapshots, see https://github.com/vitest-dev/vitest/issues/10339
`test/snapshotErrorCause.ts`,
// Normalize absolute/Windows/posix/temp paths across machines/CI.
`test/snapshotPathNormalizer.ts`,
// Turns ANSI escape sequences into human-readable `<cyan>…</color>` tags
'jest-serializer-ansi-escapes',
// Serializes react-helmet-async output predictably.
'jest-serializer-react-helmet-async',
// BOTTOM: applied first
],
reporters: process.env.GITHUB_ACTIONS
? ['default', 'github-actions']
: ['default'],
},
});