|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | + |
| 4 | +import { parseConfigFileTextToJson } from 'typescript'; |
| 5 | + |
| 6 | +function getSetupFiles(cwd: string): string[] { |
| 7 | + const packagePath = path.join(cwd, 'package.json'); |
| 8 | + |
| 9 | + let setupFiles: string[] = []; |
| 10 | + |
| 11 | + if (fs.existsSync(packagePath)) { |
| 12 | + const pkg = JSON.parse(fs.readFileSync(packagePath, 'utf8')) as { |
| 13 | + jest?: { setupFiles?: string[] }; |
| 14 | + }; |
| 15 | + |
| 16 | + setupFiles = pkg.jest?.setupFiles ?? []; |
| 17 | + } |
| 18 | + |
| 19 | + return setupFiles.map((file: string) => |
| 20 | + path.resolve(cwd, file.replace('<rootDir>', '.')), |
| 21 | + ); |
| 22 | +} |
| 23 | + |
| 24 | +export function getVitestConfig() { |
| 25 | + const cwd = process.cwd(); |
| 26 | + const tsconfigPath = path.join(cwd, 'tsconfig.json'); |
| 27 | + const baseUrl = path.dirname(tsconfigPath); |
| 28 | + |
| 29 | + const cssMockPlugin = { |
| 30 | + name: 'arui-scripts-css-mock', |
| 31 | + enforce: 'pre' as const, |
| 32 | + load(id: string) { |
| 33 | + if (id.endsWith('.css')) { |
| 34 | + return 'export default {}'; |
| 35 | + } |
| 36 | + |
| 37 | + return undefined; |
| 38 | + }, |
| 39 | + }; |
| 40 | + const alias: Array<{ find: string | RegExp; replacement: string }> = []; |
| 41 | + |
| 42 | + if (fs.existsSync(tsconfigPath)) { |
| 43 | + const tsConfigText = fs.readFileSync(tsconfigPath, 'utf8'); |
| 44 | + const { config } = parseConfigFileTextToJson(tsconfigPath, tsConfigText); |
| 45 | + const paths = config?.compilerOptions?.paths as Record<string, string[]> | undefined; |
| 46 | + const tsBaseUrl = config?.compilerOptions?.baseUrl |
| 47 | + ? path.resolve(baseUrl, config.compilerOptions.baseUrl) |
| 48 | + : baseUrl; |
| 49 | + |
| 50 | + if (paths) { |
| 51 | + for (const [key, value] of Object.entries(paths)) { |
| 52 | + const pattern = key.replace(/\/\*$/, ''); |
| 53 | + const target = value[0]?.replace(/\/\*$/, '') ?? ''; |
| 54 | + |
| 55 | + alias.push({ |
| 56 | + find: pattern, |
| 57 | + replacement: path.resolve(tsBaseUrl, target), |
| 58 | + }); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + const setupFiles = getSetupFiles(cwd); |
| 64 | + |
| 65 | + return { |
| 66 | + plugins: [cssMockPlugin], |
| 67 | + resolve: { |
| 68 | + alias, |
| 69 | + }, |
| 70 | + test: { |
| 71 | + globals: true, |
| 72 | + environment: 'jsdom' as const, |
| 73 | + setupFiles, |
| 74 | + include: [ |
| 75 | + 'src/**/__tests__/**/*.{ts,tsx,js,jsx}', |
| 76 | + 'src/**/__test__/**/*.{ts,tsx,js,jsx}', |
| 77 | + 'src/**/*.{test,spec,tests}.{ts,tsx,js,jsx}', |
| 78 | + ], |
| 79 | + exclude: ['**/node_modules/**', '**/build/**', '**/.build/**'], |
| 80 | + coverage: { |
| 81 | + provider: 'v8' as const, |
| 82 | + include: ['src/**/*.{js,jsx,ts,tsx}'], |
| 83 | + exclude: ['**/*.d.ts', '**/__tests__/**'], |
| 84 | + }, |
| 85 | + environmentOptions: { |
| 86 | + jsdom: { |
| 87 | + url: 'http://localhost', |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }; |
| 92 | +} |
0 commit comments