2024-03-31 02:07:18 +00:00
|
|
|
/// <reference types="vitest" />
|
|
|
|
|
2023-12-21 12:39:11 +00:00
|
|
|
import react from '@vitejs/plugin-react';
|
|
|
|
import fs from 'fs';
|
2024-05-18 01:24:03 +00:00
|
|
|
import path, { dirname, resolve } from 'path';
|
|
|
|
import { fileURLToPath } from 'url';
|
|
|
|
import { mergeConfig, defineConfig as vitestConfig } from 'vitest/config';
|
2024-03-31 02:07:18 +00:00
|
|
|
|
2024-05-18 01:24:03 +00:00
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
|
|
const __dirname = dirname(__filename);
|
2023-12-21 12:39:11 +00:00
|
|
|
|
2024-05-18 01:24:03 +00:00
|
|
|
const CORE_CLIENT_PACKAGES = ['sdk', 'client'];
|
2023-12-21 12:39:11 +00:00
|
|
|
|
|
|
|
const relativePathToAbsolute = (relativePath) => {
|
|
|
|
return path.resolve(process.cwd(), relativePath);
|
|
|
|
};
|
|
|
|
|
|
|
|
function tsConfigPathsToAlias() {
|
|
|
|
const json = JSON.parse(fs.readFileSync(path.resolve(process.cwd(), './tsconfig.paths.json'), { encoding: 'utf8' }));
|
|
|
|
const paths = json.compilerOptions.paths;
|
|
|
|
const alias = Object.keys(paths).reduce((acc, key) => {
|
|
|
|
if (key !== '@@/*') {
|
|
|
|
const value = paths[key][0];
|
|
|
|
acc.push({
|
|
|
|
find: key,
|
|
|
|
replacement: value,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, []);
|
2024-01-02 04:17:46 +00:00
|
|
|
alias.unshift(
|
|
|
|
{
|
|
|
|
find: '@nocobase/utils/plugin-symlink',
|
|
|
|
replacement: 'node_modules/@nocobase/utils/plugin-symlink.js',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
find: '@opentelemetry/resources',
|
|
|
|
replacement: 'node_modules/@opentelemetry/resources/build/src/index.js',
|
|
|
|
},
|
|
|
|
);
|
2023-12-21 12:39:11 +00:00
|
|
|
return [
|
|
|
|
{ find: /^~antd\/(.*)/, replacement: 'antd/$1' },
|
|
|
|
...alias.map((item) => {
|
|
|
|
return {
|
|
|
|
...item,
|
|
|
|
replacement: relativePathToAbsolute(item.replacement),
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-03-31 02:07:18 +00:00
|
|
|
const defineCommonConfig = () => {
|
|
|
|
return vitestConfig({
|
|
|
|
root: process.cwd(),
|
|
|
|
resolve: {
|
|
|
|
mainFields: ['module'],
|
|
|
|
},
|
|
|
|
test: {
|
|
|
|
globals: true,
|
|
|
|
alias: tsConfigPathsToAlias(),
|
|
|
|
testTimeout: 300000,
|
|
|
|
hookTimeout: 300000,
|
|
|
|
silent: !!process.env.GITHUB_ACTIONS,
|
2024-04-05 04:11:51 +00:00
|
|
|
include: ['packages/**/src/**/__tests__/**/*.test.{ts,tsx}'],
|
2024-03-31 02:07:18 +00:00
|
|
|
exclude: [
|
2024-04-03 01:58:30 +00:00
|
|
|
'**/demos/**',
|
2024-03-31 02:07:18 +00:00
|
|
|
'**/node_modules/**',
|
|
|
|
'**/dist/**',
|
|
|
|
'**/lib/**',
|
|
|
|
'**/es/**',
|
|
|
|
'**/.dumi/**',
|
|
|
|
'**/e2e/**',
|
|
|
|
'**/__e2e__/**',
|
|
|
|
'**/{vitest,commitlint}.config.*',
|
|
|
|
],
|
|
|
|
watchExclude: [
|
|
|
|
'**/node_modules/**',
|
|
|
|
'**/dist/**',
|
|
|
|
'**/lib/**',
|
|
|
|
'**/es/**',
|
|
|
|
'**/.dumi/**',
|
|
|
|
'**/e2e/**',
|
|
|
|
'**/__e2e__/**',
|
|
|
|
'**/{vitest,commitlint}.config.*',
|
|
|
|
],
|
|
|
|
coverage: {
|
|
|
|
provider: 'istanbul',
|
2024-04-05 04:11:51 +00:00
|
|
|
include: ['packages/**/src/**/*.{ts,tsx}'],
|
2024-04-01 14:35:28 +00:00
|
|
|
exclude: [
|
2024-04-03 01:58:30 +00:00
|
|
|
'**/demos/**',
|
2024-04-01 14:35:28 +00:00
|
|
|
'**/swagger/**',
|
|
|
|
'**/.dumi/**',
|
|
|
|
'**/.umi/**',
|
|
|
|
'**/.plugins/**',
|
|
|
|
'**/lib/**',
|
|
|
|
'**/__tests__/**',
|
|
|
|
'**/e2e/**',
|
2024-04-12 13:55:37 +00:00
|
|
|
'**/__e2e__/**',
|
2024-04-01 14:35:28 +00:00
|
|
|
'**/client.js',
|
|
|
|
'**/server.js',
|
2024-04-05 04:11:51 +00:00
|
|
|
'**/*.d.ts',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
2024-03-31 02:07:18 +00:00
|
|
|
|
|
|
|
function getExclude(isServer) {
|
|
|
|
return [
|
|
|
|
`packages/core/${isServer ? '' : '!'}(${CORE_CLIENT_PACKAGES.join('|')})/**/*`,
|
|
|
|
`packages/**/src/${isServer ? 'client' : 'server'}/**/*`,
|
2024-04-05 04:11:51 +00:00
|
|
|
];
|
2024-03-31 02:07:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const defineServerConfig = () => {
|
|
|
|
return vitestConfig({
|
|
|
|
test: {
|
|
|
|
setupFiles: resolve(__dirname, './setup/server.ts'),
|
2024-04-05 04:11:51 +00:00
|
|
|
exclude: getExclude(true),
|
2024-05-27 03:43:26 +00:00
|
|
|
retry: process.env.CI ? 2 : 0,
|
2024-03-31 02:07:18 +00:00
|
|
|
},
|
|
|
|
coverage: {
|
2024-04-05 04:11:51 +00:00
|
|
|
exclude: getExclude(true),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
2024-03-31 02:07:18 +00:00
|
|
|
|
|
|
|
const defineClientConfig = () => {
|
|
|
|
return vitestConfig({
|
|
|
|
plugins: [react()],
|
|
|
|
define: {
|
|
|
|
'process.env.__TEST__': true,
|
|
|
|
'process.env.__E2E__': false,
|
2024-04-05 04:11:51 +00:00
|
|
|
global: 'window',
|
2024-03-31 02:07:18 +00:00
|
|
|
},
|
|
|
|
test: {
|
|
|
|
environment: 'jsdom',
|
|
|
|
css: false,
|
|
|
|
setupFiles: resolve(__dirname, './setup/client.ts'),
|
|
|
|
server: {
|
|
|
|
deps: {
|
|
|
|
inline: ['@juggle/resize-observer', 'clsx'],
|
2023-12-21 12:39:11 +00:00
|
|
|
},
|
2024-03-31 02:07:18 +00:00
|
|
|
},
|
|
|
|
exclude: getExclude(false),
|
|
|
|
coverage: {
|
2024-04-05 04:11:51 +00:00
|
|
|
exclude: getExclude(false),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
2024-03-31 02:07:18 +00:00
|
|
|
|
|
|
|
export const getFilterInclude = (isServer, isCoverage) => {
|
2024-05-18 01:24:03 +00:00
|
|
|
let argv = process.argv.slice(2);
|
2024-04-13 12:45:12 +00:00
|
|
|
|
2024-05-18 01:24:03 +00:00
|
|
|
argv = argv.filter((item, index) => {
|
|
|
|
if (!item.startsWith('-')) {
|
|
|
|
const pre = argv[index - 1];
|
2024-04-13 12:45:12 +00:00
|
|
|
|
2024-05-18 01:24:03 +00:00
|
|
|
if (pre && pre.startsWith('--') && ['--reporter'].includes(pre)) {
|
|
|
|
return false;
|
2024-04-13 12:45:12 +00:00
|
|
|
}
|
|
|
|
|
2024-05-18 01:24:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
2024-04-13 12:45:12 +00:00
|
|
|
|
2024-05-18 01:24:03 +00:00
|
|
|
return false;
|
|
|
|
});
|
2024-04-13 12:45:12 +00:00
|
|
|
|
|
|
|
let filterFileOrDir = argv[0];
|
|
|
|
|
2024-04-12 13:55:37 +00:00
|
|
|
if (!filterFileOrDir) return {};
|
2024-03-31 02:07:18 +00:00
|
|
|
const absPath = path.join(process.cwd(), filterFileOrDir);
|
|
|
|
const isDir = fs.existsSync(absPath) && fs.statSync(absPath).isDirectory();
|
2024-04-26 09:44:59 +00:00
|
|
|
|
2024-03-31 02:07:18 +00:00
|
|
|
// 如果是文件,则只测试当前文件
|
|
|
|
if (!isDir) {
|
2024-04-12 13:55:37 +00:00
|
|
|
return {
|
|
|
|
isFile: true,
|
|
|
|
include: [filterFileOrDir],
|
|
|
|
};
|
2024-03-31 02:07:18 +00:00
|
|
|
}
|
|
|
|
|
2024-04-05 04:11:51 +00:00
|
|
|
const suffix = isCoverage ? `**/*.{ts,tsx}` : `**/__tests__/**/*.{test,spec}.{ts,tsx}`;
|
2024-03-31 02:07:18 +00:00
|
|
|
|
|
|
|
// 判断是否为包目录,如果不是包目录,则只测试当前目录
|
|
|
|
const isPackage = fs.existsSync(path.join(absPath, 'package.json'));
|
2024-04-26 09:44:59 +00:00
|
|
|
|
2024-03-31 02:07:18 +00:00
|
|
|
if (!isPackage) {
|
2024-04-12 13:55:37 +00:00
|
|
|
return {
|
|
|
|
include: [`${filterFileOrDir}/${suffix}`],
|
|
|
|
};
|
2024-03-31 02:07:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 判断是否为 core 包目录,不分 client 和 server
|
|
|
|
const isCore = absPath.includes('packages/core');
|
|
|
|
if (isCore) {
|
2024-04-12 13:55:37 +00:00
|
|
|
return {
|
|
|
|
include: [`${filterFileOrDir}/src/${suffix}`],
|
|
|
|
};
|
2024-03-31 02:07:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 插件目录,区分 client 和 server
|
2024-04-12 13:55:37 +00:00
|
|
|
return {
|
|
|
|
include: [`${filterFileOrDir}/src/${isServer ? 'server' : 'client'}/${suffix}`],
|
|
|
|
};
|
2024-04-05 04:11:51 +00:00
|
|
|
};
|
2024-03-31 02:07:18 +00:00
|
|
|
|
|
|
|
export const getReportsDirectory = (isServer) => {
|
2024-04-05 04:11:51 +00:00
|
|
|
let filterFileOrDir = process.argv.slice(2).find((arg) => !arg.startsWith('-'));
|
2024-03-31 02:07:18 +00:00
|
|
|
if (!filterFileOrDir) return;
|
2024-04-12 13:55:37 +00:00
|
|
|
const basePath = `./storage/coverage/`;
|
2024-03-31 02:07:18 +00:00
|
|
|
const isPackage = fs.existsSync(path.join(process.cwd(), filterFileOrDir, 'package.json'));
|
|
|
|
if (isPackage) {
|
2024-04-12 13:55:37 +00:00
|
|
|
let reportsDirectory = `${basePath}${filterFileOrDir.replace('packages/', '')}`;
|
2024-03-31 02:07:18 +00:00
|
|
|
|
|
|
|
const isCore = filterFileOrDir.includes('packages/core');
|
|
|
|
|
|
|
|
if (!isCore) {
|
|
|
|
reportsDirectory = `${reportsDirectory}/${isServer ? 'server' : 'client'}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return reportsDirectory;
|
2024-04-12 13:55:37 +00:00
|
|
|
} else {
|
2024-04-15 03:56:12 +00:00
|
|
|
return `${basePath}${filterFileOrDir.replace('packages/', '').replace('src/', '')}`;
|
2024-03-31 02:07:18 +00:00
|
|
|
}
|
2024-04-05 04:11:51 +00:00
|
|
|
};
|
2024-03-31 02:07:18 +00:00
|
|
|
|
|
|
|
export const defineConfig = () => {
|
|
|
|
const isServer = process.env.TEST_ENV === 'server-side';
|
2024-04-05 04:11:51 +00:00
|
|
|
const config = vitestConfig(
|
|
|
|
mergeConfig(defineCommonConfig(), isServer ? defineServerConfig() : defineClientConfig()),
|
|
|
|
);
|
2024-03-31 02:07:18 +00:00
|
|
|
|
2024-04-12 13:55:37 +00:00
|
|
|
const { isFile, include: filterInclude } = getFilterInclude(isServer);
|
2024-04-13 12:45:12 +00:00
|
|
|
|
2024-04-12 13:55:37 +00:00
|
|
|
if (filterInclude) {
|
|
|
|
config.test.include = filterInclude;
|
2024-04-26 09:44:59 +00:00
|
|
|
|
2024-04-12 13:55:37 +00:00
|
|
|
if (isFile) {
|
|
|
|
// 减少收集的文件
|
|
|
|
config.test.exclude = [];
|
2024-04-13 12:45:12 +00:00
|
|
|
|
2024-04-12 13:55:37 +00:00
|
|
|
config.test.coverage = {
|
|
|
|
enabled: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-31 02:07:18 +00:00
|
|
|
const isCoverage = process.argv.includes('--coverage');
|
2024-04-26 09:44:59 +00:00
|
|
|
|
2024-03-31 02:07:18 +00:00
|
|
|
if (!isCoverage) {
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2024-04-12 13:55:37 +00:00
|
|
|
const { include: coverageInclude } = getFilterInclude(isServer, true);
|
2024-05-18 01:24:03 +00:00
|
|
|
|
2024-04-12 13:55:37 +00:00
|
|
|
if (coverageInclude) {
|
|
|
|
config.test.coverage.include = coverageInclude;
|
2024-03-31 11:13:58 +00:00
|
|
|
}
|
2024-04-26 09:44:59 +00:00
|
|
|
|
2024-03-31 02:07:18 +00:00
|
|
|
const reportsDirectory = getReportsDirectory(isServer);
|
|
|
|
if (reportsDirectory) {
|
|
|
|
config.test.coverage.reportsDirectory = reportsDirectory;
|
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
2023-12-21 12:39:11 +00:00
|
|
|
};
|