-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.config.ts
More file actions
97 lines (87 loc) · 3.04 KB
/
vitest.config.ts
File metadata and controls
97 lines (87 loc) · 3.04 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
import { defineConfig } from 'vitest/config';
import react from '@vitejs/plugin-react';
import fs from 'fs';
import path from 'path';
// Helper function to resolve with possible extensions
function resolveWithExtensions(basePath: string): string | null {
const extensions = ['.tsx', '.ts', '.js']; // Add any other extensions you need
for (const ext of extensions) {
const fullPath = `${basePath}/index${ext}`;
if (fs.existsSync(fullPath)) {
return fullPath;
}
}
return null;
}
/**
* Some theme components are 'swizzled' so are imported from the `src/theme` directory.
* However, unswizzled component files are imported from `node_modules/@docusaurus/
* theme-classic`. This plugin will map these imports accordingly in tests.
*/
function ThemeAliasPlugin() {
return {
name: 'vite-plugin-theme-alias',
resolveId(source: string) {
if (source.startsWith('@theme/')) {
const aliasPath = source.replace('@theme/', '');
// Check for the file in `src/theme` directory first
const primaryPath = path.resolve(__dirname, 'src/theme', aliasPath);
const resolvedPrimaryPath = resolveWithExtensions(primaryPath);
if (resolvedPrimaryPath) {
return resolvedPrimaryPath;
}
// If not found, fall back to `@docusaurus/theme-classic/src/theme`
const fallbackPaths = [
path.resolve(__dirname, 'node_modules/@docusaurus/theme-classic/src/theme', aliasPath),
path.resolve(__dirname, 'node_modules/@docusaurus/theme-classic/lib/theme', aliasPath),
];
for (const fallbackPath of fallbackPaths) {
const resolvedFallback = resolveWithExtensions(fallbackPath);
if (resolvedFallback) {
return resolvedFallback;
}
}
// Return null if neither path resolves (Vitest will throw a module not found error)
return null;
}
return null;
},
};
}
export default defineConfig({
plugins: [react(), ThemeAliasPlugin()],
resolve: {
alias: [
{
find: '@site',
replacement: path.resolve(__dirname),
},
{
// @docusaurus/Noop is a little special
find: '@docusaurus/Noop',
replacement: path.resolve(__dirname, './test/__mocks__/noop.ts'),
},
{
// Match any import that starts with `@docusaurus/` and map to core client exports
find: /^@docusaurus\/(BrowserOnly|ComponentCreator|constants|ExecutionEnvironment|Head|Interpolate|isInternalUrl|Link|renderRoutes|router|Translate)/,
replacement: '@docusaurus/core/src/client/exports/$1',
},
{
find: '@docusaurus/useDocusaurusContext',
replacement: path.resolve(
__dirname,
'node_modules/@docusaurus/core/lib/client/exports/useDocusaurusContext.js',
),
},
{
find: '@docusaurus/useBaseUrl',
replacement: path.resolve(__dirname, './test/__mocks__/useBaseUrl.js'),
},
],
},
test: {
globals: true,
environment: 'happy-dom',
setupFiles: './test/setup.ts',
},
});