test: migrate test runner to vitest (#12206)

This commit is contained in:
neverland
2023-08-20 18:48:09 +08:00
committed by GitHub
parent 633e48a645
commit 694daef6ea
374 changed files with 38189 additions and 31347 deletions

View File

@@ -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(),
};

View File

@@ -1 +0,0 @@
module.exports = 'test-file-stub';

View File

@@ -1 +0,0 @@
require('jest-canvas-mock');

View File

@@ -1 +0,0 @@
module.exports = {};

View File

@@ -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),
};
},
};