|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | + |
| 4 | +import tsconfigPaths from 'vite-tsconfig-paths'; |
| 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) => path.resolve(cwd, file.replace('<rootDir>', '.'))); |
| 20 | +} |
| 21 | + |
| 22 | +const ASSET_EXTENSIONS = [ |
| 23 | + '.svg', |
| 24 | + '.png', |
| 25 | + '.jpg', |
| 26 | + '.jpeg', |
| 27 | + '.gif', |
| 28 | + '.webp', |
| 29 | + '.ico', |
| 30 | + '.bmp', |
| 31 | + '.woff', |
| 32 | + '.woff2', |
| 33 | + '.ttf', |
| 34 | + '.eot', |
| 35 | +]; |
| 36 | + |
| 37 | +function isAsset(id: string): boolean { |
| 38 | + return ASSET_EXTENSIONS.some((extension) => id.endsWith(extension)); |
| 39 | +} |
| 40 | + |
| 41 | +const staticFilesMockPlugin = { |
| 42 | + name: 'arui-scripts-static-files-mock', |
| 43 | + enforce: 'pre' as const, |
| 44 | + load(id: string) { |
| 45 | + if (id.endsWith('.css')) { |
| 46 | + return 'export default {}'; |
| 47 | + } |
| 48 | + |
| 49 | + if (isAsset(id)) { |
| 50 | + const filename = path.basename(id); |
| 51 | + |
| 52 | + return `export default ${JSON.stringify(filename)}`; |
| 53 | + } |
| 54 | + |
| 55 | + return undefined; |
| 56 | + }, |
| 57 | +}; |
| 58 | + |
| 59 | +export function getVitestConfig() { |
| 60 | + const cwd = process.cwd(); |
| 61 | + const setupFiles = getSetupFiles(cwd); |
| 62 | + |
| 63 | + return { |
| 64 | + plugins: [tsconfigPaths({ root: cwd }), staticFilesMockPlugin], |
| 65 | + test: { |
| 66 | + environment: 'jsdom' as const, |
| 67 | + setupFiles, |
| 68 | + include: [ |
| 69 | + 'src/**/__tests__/**/*.{ts,tsx,js,jsx}', |
| 70 | + 'src/**/__test__/**/*.{ts,tsx,js,jsx}', |
| 71 | + 'src/**/*.{test,spec,tests}.{ts,tsx,js,jsx}', |
| 72 | + ], |
| 73 | + exclude: ['**/node_modules/**', '**/build/**', '**/.build/**'], |
| 74 | + coverage: { |
| 75 | + provider: 'v8' as const, |
| 76 | + include: ['src/**/*.{js,jsx,ts,tsx}'], |
| 77 | + exclude: ['**/*.d.ts', '**/__tests__/**'], |
| 78 | + }, |
| 79 | + environmentOptions: { |
| 80 | + jsdom: { |
| 81 | + url: 'http://localhost', |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }; |
| 86 | +} |
0 commit comments