test: migrate test runner to vitest (#12206)
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
## Unreleased
|
||||
|
||||
- 移除 vant-cli test 命令
|
||||
- 移除 vant-cli release 命令
|
||||
- 移除 vant-cli changelog 命令
|
||||
- 升级 commander v11
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
const { join } = require('path');
|
||||
const { existsSync } = require('fs');
|
||||
const { ROOT } = require('./shared.cjs');
|
||||
|
||||
const JEST_SETUP_FILE = join(__dirname, 'jest.setup.cjs');
|
||||
const JEST_FILE_MOCK_FILE = join(__dirname, 'jest.file-mock.cjs');
|
||||
const JEST_STYLE_MOCK_FILE = join(__dirname, 'jest.style-mock.cjs');
|
||||
|
||||
const DEFAULT_CONFIG = {
|
||||
testEnvironment: 'jsdom',
|
||||
moduleNameMapper: {
|
||||
'\\.(css|less|scss)$': JEST_STYLE_MOCK_FILE,
|
||||
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
|
||||
JEST_FILE_MOCK_FILE,
|
||||
},
|
||||
setupFilesAfterEnv: [JEST_SETUP_FILE],
|
||||
moduleFileExtensions: ['js', 'jsx', 'vue', 'ts', 'tsx'],
|
||||
transform: {
|
||||
'\\.(js|jsx|ts|tsx|vue)$':
|
||||
'<rootDir>/node_modules/@vant/cli/cjs/jest.transformer.cjs',
|
||||
},
|
||||
transformIgnorePatterns: ['/node_modules/(?!(@vant/cli))/'],
|
||||
snapshotSerializers: ['jest-serializer-html'],
|
||||
collectCoverage: true,
|
||||
collectCoverageFrom: [
|
||||
'src/**/*.{js,jsx,ts,tsx,vue}',
|
||||
'!**/demo/**',
|
||||
'!**/test/**',
|
||||
],
|
||||
coverageReporters: ['html', 'lcov', 'text-summary'],
|
||||
coverageDirectory: './test/coverage',
|
||||
testEnvironmentOptions: {
|
||||
// https://stackoverflow.com/questions/72428323/jest-referenceerror-vue-is-not-defined
|
||||
customExportConditions: ['node', 'node-addons'],
|
||||
},
|
||||
};
|
||||
|
||||
function readRootConfig() {
|
||||
const ROOT_CONFIG_PATH = join(ROOT, 'jest.config.js');
|
||||
|
||||
if (existsSync(ROOT_CONFIG_PATH)) {
|
||||
return require(ROOT_CONFIG_PATH);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
...DEFAULT_CONFIG,
|
||||
...readRootConfig(),
|
||||
};
|
||||
@@ -1 +0,0 @@
|
||||
module.exports = 'test-file-stub';
|
||||
@@ -1 +0,0 @@
|
||||
require('jest-canvas-mock');
|
||||
@@ -1 +0,0 @@
|
||||
module.exports = {};
|
||||
@@ -1,94 +0,0 @@
|
||||
const sfc = require('vue/compiler-sfc');
|
||||
const babel = require('@babel/core');
|
||||
const esbuild = require('esbuild');
|
||||
const nodePath = require('path');
|
||||
|
||||
const isJsxFile = (path) => /\.(j|t)sx$/.test(path);
|
||||
const isTsxFile = (path) => /\.tsx$/.test(path);
|
||||
const isVueFile = (path) => /\.vue$/.test(path);
|
||||
|
||||
const transformJsx = (code, path) => {
|
||||
const babelResult = babel.transformSync(code, {
|
||||
filename: path,
|
||||
babelrc: false,
|
||||
presets: isTsxFile(path) ? ['@babel/preset-typescript'] : [],
|
||||
plugins: [['@vue/babel-plugin-jsx']],
|
||||
});
|
||||
return babelResult?.code || '';
|
||||
};
|
||||
|
||||
const transformSFC = (code, path) => {
|
||||
const parsedPath = nodePath.parse(path);
|
||||
const { descriptor, errors } = sfc.parse(code, {
|
||||
filename: parsedPath.base,
|
||||
sourceRoot: parsedPath.dir,
|
||||
});
|
||||
|
||||
if (errors.length) {
|
||||
errors.forEach((error) => console.error(error));
|
||||
return '';
|
||||
}
|
||||
|
||||
const output = [];
|
||||
let bindingMetadata = {};
|
||||
|
||||
if (descriptor.script) {
|
||||
const content = descriptor.script.content.replace(
|
||||
'export default',
|
||||
'const script =',
|
||||
);
|
||||
output.push(content);
|
||||
} else if (descriptor.scriptSetup) {
|
||||
const result = sfc.compileScript(descriptor, {
|
||||
id: 'mock',
|
||||
});
|
||||
|
||||
const content = result.content.replace('export default', 'const script =');
|
||||
output.push(content);
|
||||
|
||||
if (result.bindings) {
|
||||
bindingMetadata = result.bindings;
|
||||
}
|
||||
} else {
|
||||
output.push(`const script = {};`);
|
||||
}
|
||||
|
||||
if (descriptor.template) {
|
||||
const render = sfc.compileTemplate({
|
||||
id: 'mock',
|
||||
source: descriptor.template.content,
|
||||
filename: path,
|
||||
compilerOptions: {
|
||||
bindingMetadata,
|
||||
},
|
||||
}).code;
|
||||
output.push(render);
|
||||
output.push('script.render = render;');
|
||||
}
|
||||
|
||||
output.push('export default script;');
|
||||
|
||||
return output.join('\n');
|
||||
};
|
||||
|
||||
const transformScript = (code) =>
|
||||
esbuild.transformSync(code, {
|
||||
target: 'es2016',
|
||||
format: 'cjs',
|
||||
loader: 'ts',
|
||||
}).code;
|
||||
|
||||
module.exports = {
|
||||
canInstrument: true,
|
||||
process(code, path) {
|
||||
if (isVueFile(path)) {
|
||||
code = transformSFC(code, path);
|
||||
}
|
||||
if (isJsxFile(path)) {
|
||||
code = transformJsx(code, path);
|
||||
}
|
||||
return {
|
||||
code: transformScript(code),
|
||||
};
|
||||
},
|
||||
};
|
||||
@@ -38,7 +38,6 @@
|
||||
"author": "chenjiahan",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@jest/types": "^29.1.2",
|
||||
"@types/fs-extra": "^11.0.1",
|
||||
"@types/less": "^3.0.3",
|
||||
"@types/lodash": "^4.14.191",
|
||||
@@ -49,7 +48,6 @@
|
||||
"dependencies": {
|
||||
"@babel/core": "^7.18.13",
|
||||
"@babel/preset-typescript": "^7.18.6",
|
||||
"@types/jest": "^29.5.1",
|
||||
"@vant/eslint-config": "^4.0.0",
|
||||
"@vant/touch-emulator": "^1.4.0",
|
||||
"@vitejs/plugin-vue": "^4.0.0",
|
||||
@@ -66,10 +64,6 @@
|
||||
"hash-sum": "^2.0.0",
|
||||
"highlight.js": "^11.6.0",
|
||||
"husky": "^8.0.1",
|
||||
"jest": "^29.5.0",
|
||||
"jest-canvas-mock": "^2.4.0",
|
||||
"jest-environment-jsdom": "^29.1.2",
|
||||
"jest-serializer-html": "^7.1.0",
|
||||
"less": "^4.1.3",
|
||||
"lodash": "^4.17.21",
|
||||
"markdown-it": "^13.0.1",
|
||||
|
||||
@@ -22,39 +22,6 @@ program
|
||||
return lint();
|
||||
});
|
||||
|
||||
program
|
||||
.command('test')
|
||||
.description('Run unit tests through jest')
|
||||
.option(
|
||||
'--watch',
|
||||
'Watch files for changes and rerun tests related to changed files',
|
||||
)
|
||||
.option(
|
||||
'--clearCache',
|
||||
'Clears the configured Jest cache directory and then exits',
|
||||
)
|
||||
.option(
|
||||
'--changedSince <changedSince>',
|
||||
'Runs tests related to the changes since the provided branch or commit hash',
|
||||
)
|
||||
.option(
|
||||
'--logHeapUsage',
|
||||
'Logs the heap usage after every test. Useful to debug memory leaks',
|
||||
)
|
||||
.option(
|
||||
'--runInBand',
|
||||
'Run all tests serially in the current process, rather than creating a worker pool of child processes that run tests',
|
||||
)
|
||||
.option(
|
||||
'--updateSnapshot',
|
||||
'Re-record every snapshot that fails during this test run',
|
||||
)
|
||||
.option('--debug', 'Print debugging info about your Jest config')
|
||||
.action(async (options) => {
|
||||
const { test } = await import('./commands/jest.js');
|
||||
return test(options);
|
||||
});
|
||||
|
||||
program
|
||||
.command('clean')
|
||||
.description('Clean all dist files')
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
import jest from 'jest';
|
||||
import { setNodeEnv } from '../common/index.js';
|
||||
import { genPackageEntry } from '../compiler/gen-package-entry.js';
|
||||
import {
|
||||
ROOT,
|
||||
JEST_CONFIG_FILE,
|
||||
PACKAGE_ENTRY_FILE,
|
||||
} from '../common/constant.js';
|
||||
import type { Config } from '@jest/types';
|
||||
|
||||
export function test(command: Config.Argv) {
|
||||
setNodeEnv('test');
|
||||
|
||||
genPackageEntry({
|
||||
outputPath: PACKAGE_ENTRY_FILE,
|
||||
});
|
||||
|
||||
const config = {
|
||||
rootDir: ROOT,
|
||||
watch: command.watch,
|
||||
debug: command.debug,
|
||||
config: JEST_CONFIG_FILE,
|
||||
runInBand: command.runInBand,
|
||||
clearCache: command.clearCache,
|
||||
changedSince: command.changedSince,
|
||||
logHeapUsage: command.logHeapUsage,
|
||||
updateSnapshot: command.updateSnapshot,
|
||||
// make jest tests faster
|
||||
// see: https://ivantanev.com/make-jest-faster/
|
||||
maxWorkers: '50%',
|
||||
} as Config.Argv;
|
||||
|
||||
jest
|
||||
.runCLI(config, [ROOT])
|
||||
.then((response) => {
|
||||
if (!response.results.success && !command.watch) {
|
||||
process.exit(1);
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
|
||||
if (!command.watch) {
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -40,7 +40,6 @@ export const STYLE_DEPS_JSON_FILE = join(DIST_DIR, 'style-deps.json');
|
||||
|
||||
// Config files
|
||||
export const POSTCSS_CONFIG_FILE = join(CJS_DIR, 'postcss.config.cjs');
|
||||
export const JEST_CONFIG_FILE = join(CJS_DIR, 'jest.config.cjs');
|
||||
|
||||
export const SCRIPT_EXTS = [
|
||||
'.js',
|
||||
|
||||
Reference in New Issue
Block a user