nocobase/packages/core/test/vitest.mjs

245 lines
6.3 KiB
JavaScript
Raw Normal View History

/// <reference types="vitest" />
refactor: establish a sound testing system (#3179) * chore: use vitest to replace jest * chore: support vitest * feat: vitest 1.0 * fix: test * chore: yarn.lock * chore: github actions * fix: test * fix: test * fix: test * fix: test * fix: jest.fn * fix: require * fix: test * fix: build * fix: test * fix: test * fix: test * fix: test * fix: test * fix: test * fix: test * fix: dynamic import * fix: bug * chore: yarn run test command * chore: package.json * chore: package.json * chore: vite 5 * fix: fix variable test * fix: import json * feat: initEnv * fix: env.APP_ENV_PATH * chore: get package json * fix: remove GlobalThmeProvider * chore: update snap * chore: test env * chore: test env * chore: import module * chore: jest * fix: load package json * chore: test * fix: bug * chore: test * chore: test * chore: test * chore: test * chore: test * fix: import file in windows * chore: import module with absolute file path * fix: test error * test: update snapshot * chore: update yarn.lock * fix: front-end tests do not include utils folder * refactor: use vitest-dom * fix: fix build * fix: test error * fix: change to vitest.config.mts * fix: types error * fix: types error * fix: types error * fix: error * fix: test * chore: test * fix: test package * feat: update dependencies * refactor: test * fix: error * fix: error * fix: __dirname is not defined in ES module scope * fix: allow only * fix: error * fix: error * fix: error * fix: create-app * fix: install-deps * feat: update docs --------- Co-authored-by: chenos <chenlinxh@gmail.com> Co-authored-by: dream2023 <1098626505@qq.com> Co-authored-by: Zeke Zhang <958414905@qq.com>
2023-12-21 12:39:11 +00:00
import react from '@vitejs/plugin-react';
import fs from 'fs';
import path, { resolve } from 'path';
import { URL } from 'url';
import { mergeConfig, defineConfig as vitestConfig } from 'vitest/config';
const CORE_CLIENT_PACKAGES = ['sdk', 'client'];
refactor: establish a sound testing system (#3179) * chore: use vitest to replace jest * chore: support vitest * feat: vitest 1.0 * fix: test * chore: yarn.lock * chore: github actions * fix: test * fix: test * fix: test * fix: test * fix: jest.fn * fix: require * fix: test * fix: build * fix: test * fix: test * fix: test * fix: test * fix: test * fix: test * fix: test * fix: dynamic import * fix: bug * chore: yarn run test command * chore: package.json * chore: package.json * chore: vite 5 * fix: fix variable test * fix: import json * feat: initEnv * fix: env.APP_ENV_PATH * chore: get package json * fix: remove GlobalThmeProvider * chore: update snap * chore: test env * chore: test env * chore: import module * chore: jest * fix: load package json * chore: test * fix: bug * chore: test * chore: test * chore: test * chore: test * chore: test * fix: import file in windows * chore: import module with absolute file path * fix: test error * test: update snapshot * chore: update yarn.lock * fix: front-end tests do not include utils folder * refactor: use vitest-dom * fix: fix build * fix: test error * fix: change to vitest.config.mts * fix: types error * fix: types error * fix: types error * fix: error * fix: test * chore: test * fix: test package * feat: update dependencies * refactor: test * fix: error * fix: error * fix: __dirname is not defined in ES module scope * fix: allow only * fix: error * fix: error * fix: error * fix: create-app * fix: install-deps * feat: update docs --------- Co-authored-by: chenos <chenlinxh@gmail.com> Co-authored-by: dream2023 <1098626505@qq.com> Co-authored-by: Zeke Zhang <958414905@qq.com>
2023-12-21 12:39:11 +00:00
const __dirname = new URL('.', import.meta.url).pathname;
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;
}, []);
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',
},
);
refactor: establish a sound testing system (#3179) * chore: use vitest to replace jest * chore: support vitest * feat: vitest 1.0 * fix: test * chore: yarn.lock * chore: github actions * fix: test * fix: test * fix: test * fix: test * fix: jest.fn * fix: require * fix: test * fix: build * fix: test * fix: test * fix: test * fix: test * fix: test * fix: test * fix: test * fix: dynamic import * fix: bug * chore: yarn run test command * chore: package.json * chore: package.json * chore: vite 5 * fix: fix variable test * fix: import json * feat: initEnv * fix: env.APP_ENV_PATH * chore: get package json * fix: remove GlobalThmeProvider * chore: update snap * chore: test env * chore: test env * chore: import module * chore: jest * fix: load package json * chore: test * fix: bug * chore: test * chore: test * chore: test * chore: test * chore: test * fix: import file in windows * chore: import module with absolute file path * fix: test error * test: update snapshot * chore: update yarn.lock * fix: front-end tests do not include utils folder * refactor: use vitest-dom * fix: fix build * fix: test error * fix: change to vitest.config.mts * fix: types error * fix: types error * fix: types error * fix: error * fix: test * chore: test * fix: test package * feat: update dependencies * refactor: test * fix: error * fix: error * fix: __dirname is not defined in ES module scope * fix: allow only * fix: error * fix: error * fix: error * fix: create-app * fix: install-deps * feat: update docs --------- Co-authored-by: chenos <chenlinxh@gmail.com> Co-authored-by: dream2023 <1098626505@qq.com> Co-authored-by: Zeke Zhang <958414905@qq.com>
2023-12-21 12:39:11 +00:00
return [
{ find: /^~antd\/(.*)/, replacement: 'antd/$1' },
...alias.map((item) => {
return {
...item,
replacement: relativePathToAbsolute(item.replacement),
};
}),
];
}
const defineCommonConfig = () => {
return vitestConfig({
root: process.cwd(),
resolve: {
mainFields: ['module'],
},
test: {
globals: true,
alias: tsConfigPathsToAlias(),
testTimeout: 300000,
hookTimeout: 300000,
silent: !!process.env.GITHUB_ACTIONS,
include: ['packages/**/src/**/__tests__/**/*.test.{ts,tsx}'],
exclude: [
2024-04-03 01:58:30 +00:00
'**/demos/**',
'**/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',
include: ['packages/**/src/**/*.{ts,tsx}'],
2024-04-01 14:35:28 +00:00
exclude: [
'**/requirejs.ts',
2024-04-03 01:58:30 +00:00
'**/demos/**',
2024-04-01 14:35:28 +00:00
'**/swagger/**',
'**/.dumi/**',
'**/.umi/**',
'**/.plugins/**',
'**/lib/**',
'**/__tests__/**',
'**/e2e/**',
'**/__e2e__/**',
2024-04-01 14:35:28 +00:00
'**/client.js',
'**/server.js',
'**/*.d.ts',
],
},
},
});
};
function getExclude(isServer) {
return [
`packages/core/${isServer ? '' : '!'}(${CORE_CLIENT_PACKAGES.join('|')})/**/*`,
`packages/**/src/${isServer ? 'client' : 'server'}/**/*`,
];
}
const defineServerConfig = () => {
return vitestConfig({
test: {
setupFiles: resolve(__dirname, './setup/server.ts'),
exclude: getExclude(true),
},
coverage: {
exclude: getExclude(true),
},
});
};
const defineClientConfig = () => {
return vitestConfig({
plugins: [react()],
define: {
'process.env.__TEST__': true,
'process.env.__E2E__': false,
global: 'window',
},
test: {
environment: 'jsdom',
css: false,
setupFiles: resolve(__dirname, './setup/client.ts'),
server: {
deps: {
inline: ['@juggle/resize-observer', 'clsx'],
refactor: establish a sound testing system (#3179) * chore: use vitest to replace jest * chore: support vitest * feat: vitest 1.0 * fix: test * chore: yarn.lock * chore: github actions * fix: test * fix: test * fix: test * fix: test * fix: jest.fn * fix: require * fix: test * fix: build * fix: test * fix: test * fix: test * fix: test * fix: test * fix: test * fix: test * fix: dynamic import * fix: bug * chore: yarn run test command * chore: package.json * chore: package.json * chore: vite 5 * fix: fix variable test * fix: import json * feat: initEnv * fix: env.APP_ENV_PATH * chore: get package json * fix: remove GlobalThmeProvider * chore: update snap * chore: test env * chore: test env * chore: import module * chore: jest * fix: load package json * chore: test * fix: bug * chore: test * chore: test * chore: test * chore: test * chore: test * fix: import file in windows * chore: import module with absolute file path * fix: test error * test: update snapshot * chore: update yarn.lock * fix: front-end tests do not include utils folder * refactor: use vitest-dom * fix: fix build * fix: test error * fix: change to vitest.config.mts * fix: types error * fix: types error * fix: types error * fix: error * fix: test * chore: test * fix: test package * feat: update dependencies * refactor: test * fix: error * fix: error * fix: __dirname is not defined in ES module scope * fix: allow only * fix: error * fix: error * fix: error * fix: create-app * fix: install-deps * feat: update docs --------- Co-authored-by: chenos <chenlinxh@gmail.com> Co-authored-by: dream2023 <1098626505@qq.com> Co-authored-by: Zeke Zhang <958414905@qq.com>
2023-12-21 12:39:11 +00:00
},
},
exclude: getExclude(false),
coverage: {
exclude: getExclude(false),
},
},
});
};
export const getFilterInclude = (isServer, isCoverage) => {
let filterFileOrDir = process.argv.slice(2).find((arg) => !arg.startsWith('-'));
if (!filterFileOrDir) return {};
const absPath = path.join(process.cwd(), filterFileOrDir);
const isDir = fs.existsSync(absPath) && fs.statSync(absPath).isDirectory();
// 如果是文件,则只测试当前文件
if (!isDir) {
return {
isFile: true,
include: [filterFileOrDir],
};
}
const suffix = isCoverage ? `**/*.{ts,tsx}` : `**/__tests__/**/*.{test,spec}.{ts,tsx}`;
// 判断是否为包目录,如果不是包目录,则只测试当前目录
const isPackage = fs.existsSync(path.join(absPath, 'package.json'));
if (!isPackage) {
return {
include: [`${filterFileOrDir}/${suffix}`],
};
}
// 判断是否为 core 包目录,不分 client 和 server
const isCore = absPath.includes('packages/core');
if (isCore) {
return {
include: [`${filterFileOrDir}/src/${suffix}`],
};
}
// 插件目录,区分 client 和 server
return {
include: [`${filterFileOrDir}/src/${isServer ? 'server' : 'client'}/${suffix}`],
};
};
export const getReportsDirectory = (isServer) => {
let filterFileOrDir = process.argv.slice(2).find((arg) => !arg.startsWith('-'));
if (!filterFileOrDir) return;
const basePath = `./storage/coverage/`;
const isPackage = fs.existsSync(path.join(process.cwd(), filterFileOrDir, 'package.json'));
if (isPackage) {
let reportsDirectory = `${basePath}${filterFileOrDir.replace('packages/', '')}`;
const isCore = filterFileOrDir.includes('packages/core');
if (!isCore) {
reportsDirectory = `${reportsDirectory}/${isServer ? 'server' : 'client'}`;
}
return reportsDirectory;
} else {
return basePath;
}
};
export const defineConfig = () => {
const isServer = process.env.TEST_ENV === 'server-side';
const config = vitestConfig(
mergeConfig(defineCommonConfig(), isServer ? defineServerConfig() : defineClientConfig()),
);
const { isFile, include: filterInclude } = getFilterInclude(isServer);
if (filterInclude) {
config.test.include = filterInclude;
if (isFile) {
// 减少收集的文件
config.test.exclude = [];
config.test.coverage = {
enabled: false,
};
return config;
}
}
const isCoverage = process.argv.includes('--coverage');
if (!isCoverage) {
return config;
}
const { include: coverageInclude } = getFilterInclude(isServer, true);
if (coverageInclude) {
config.test.coverage.include = coverageInclude;
2024-03-31 11:13:58 +00:00
}
const reportsDirectory = getReportsDirectory(isServer);
if (reportsDirectory) {
config.test.coverage.reportsDirectory = reportsDirectory;
}
return config;
refactor: establish a sound testing system (#3179) * chore: use vitest to replace jest * chore: support vitest * feat: vitest 1.0 * fix: test * chore: yarn.lock * chore: github actions * fix: test * fix: test * fix: test * fix: test * fix: jest.fn * fix: require * fix: test * fix: build * fix: test * fix: test * fix: test * fix: test * fix: test * fix: test * fix: test * fix: dynamic import * fix: bug * chore: yarn run test command * chore: package.json * chore: package.json * chore: vite 5 * fix: fix variable test * fix: import json * feat: initEnv * fix: env.APP_ENV_PATH * chore: get package json * fix: remove GlobalThmeProvider * chore: update snap * chore: test env * chore: test env * chore: import module * chore: jest * fix: load package json * chore: test * fix: bug * chore: test * chore: test * chore: test * chore: test * chore: test * fix: import file in windows * chore: import module with absolute file path * fix: test error * test: update snapshot * chore: update yarn.lock * fix: front-end tests do not include utils folder * refactor: use vitest-dom * fix: fix build * fix: test error * fix: change to vitest.config.mts * fix: types error * fix: types error * fix: types error * fix: error * fix: test * chore: test * fix: test package * feat: update dependencies * refactor: test * fix: error * fix: error * fix: __dirname is not defined in ES module scope * fix: allow only * fix: error * fix: error * fix: error * fix: create-app * fix: install-deps * feat: update docs --------- Co-authored-by: chenos <chenlinxh@gmail.com> Co-authored-by: dream2023 <1098626505@qq.com> Co-authored-by: Zeke Zhang <958414905@qq.com>
2023-12-21 12:39:11 +00:00
};