forked from facebook/lexical
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.config.mts
More file actions
131 lines (128 loc) · 4.05 KB
/
vitest.config.mts
File metadata and controls
131 lines (128 loc) · 4.05 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) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import react from '@vitejs/plugin-react';
import * as fs from 'node:fs';
import * as path from 'node:path';
import {fileURLToPath} from 'node:url';
import {defineConfig} from 'vitest/config';
// Resolve monorepo imports to TypeScript source from the test tsconfig's
// `paths`. This includes the cross-package and deep `*/src/__tests__/utils`
// aliases that the unit tests rely on but that the lean root tsconfig (which
// resolves via the `source` export condition) intentionally omits. Vite's
// native `resolve.tsconfigPaths` only reads the root tsconfig, so we build
// the aliases explicitly from tsconfig.test.json instead.
const ROOT_DIR = path.dirname(fileURLToPath(import.meta.url));
const escapeRegExp = (value: string) =>
value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
function tsconfigTestAliases(): {find: RegExp; replacement: string}[] {
const {compilerOptions} = JSON.parse(
fs.readFileSync(path.join(ROOT_DIR, 'tsconfig.test.json'), 'utf8'),
);
return Object.entries(
(compilerOptions.paths || {}) as Record<string, string[]>,
).map(([find, [replacement]]) => {
// Wildcard entries like `lexical/src/*` -> `./packages/lexical/src/*`
// become a capture-group alias so deep source imports resolve.
if (find.endsWith('/*') && replacement.endsWith('/*')) {
return {
find: new RegExp(`^${escapeRegExp(find.slice(0, -2))}/(.*)$`),
replacement: `${path.resolve(ROOT_DIR, replacement.slice(0, -2))}/$1`,
};
}
return {
find: new RegExp(`^${escapeRegExp(find)}$`),
replacement: path.resolve(ROOT_DIR, replacement),
};
});
}
export default defineConfig({
resolve: {
alias: tsconfigTestAliases(),
conditions: ['development', 'import', 'module', 'browser', 'default'],
dedupe: ['react', 'react-dom'],
},
test: {
clearMocks: true,
projects: [
{
define: {
// https://react.dev/blog/2022/03/08/react-18-upgrade-guide#configuring-your-testing-environment
IS_REACT_ACT_ENVIRONMENT: true,
},
extends: true,
plugins: [react()],
test: {
env: {
LEXICAL_VERSION: JSON.stringify(
`${process.env.npm_package_version}+git`,
),
},
environment: 'jsdom',
include: ['packages/**/__tests__/unit/**/*.test{.ts,.tsx,.js,.jsx}'],
name: 'unit',
setupFiles: ['./vitest.setup.mts'],
typecheck: {
tsconfig: './tsconfig.test.json',
},
},
},
{
extends: true,
test: {
environment: 'node',
include: ['scripts/**/__tests__/unit/**/*.test.ts'],
name: 'scripts-unit',
},
},
{
extends: true,
test: {
environment: 'node',
globalSetup: './scripts/__tests__/integration/setup.mjs',
include: ['scripts/__tests__/integration/**/*.test.mjs'],
name: 'integration',
},
},
{
extends: true,
test: {
benchmark: {
exclude: ['**/node_modules/**', '**/__bench__/dom/**'],
include: ['packages/*/src/__bench__/*.bench.ts'],
},
environment: 'node',
name: 'bench',
},
},
{
define: {
IS_REACT_ACT_ENVIRONMENT: true,
},
extends: true,
plugins: [react()],
test: {
benchmark: {
exclude: ['**/node_modules/**'],
include: ['packages/*/src/__bench__/dom/**/*.bench.ts'],
},
env: {
LEXICAL_VERSION: JSON.stringify(
`${process.env.npm_package_version}+git`,
),
},
environment: 'jsdom',
name: 'bench-dom',
setupFiles: ['./vitest.setup.mts'],
typecheck: {
tsconfig: './tsconfig.test.json',
},
},
},
],
},
});