-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvitest.config.ts
79 lines (71 loc) · 2.47 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
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}${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 fallbackPath = path.resolve(
__dirname,
'node_modules/@docusaurus/theme-classic/src/theme',
aliasPath,
);
const resolvedFallbackPath = resolveWithExtensions(fallbackPath);
if (resolvedFallbackPath) {
return resolvedFallbackPath;
}
// 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: [
{
// @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|use.*)/,
replacement: '@docusaurus/core/src/client/exports/$1',
},
],
},
test: {
globals: true,
environment: 'happy-dom',
setupFiles: './test/setup.ts',
},
});