Skip to content

Commit

Permalink
test: update vitest config
Browse files Browse the repository at this point in the history
  • Loading branch information
anlyyao committed Aug 14, 2022
1 parent f7ef469 commit b21b586
Show file tree
Hide file tree
Showing 27 changed files with 184 additions and 4,223 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"test:snap-update": "cross-env NODE_ENV=test-snap vitest run --config site/vite.config.js -u",
"test:e2e": "cypress run --config-file scripts/test/cypress.json",
"test:e2e-gui": "cypress open --config-file scripts/test/cypress.json",
"test:demo": "node src/_common/test/script/generate-demo-test.js",
"test:demo": "node scripts/test/generate-demo-test.js",
"lint": "eslint --ext .vue,.ts,.tsx ./ --max-warnings 0 --ignore-path .gitignore --ignore-path .eslintignore",
"lint:fix": "npm run lint:fix-prettier && npm run lint:fix-eslint",
"lint:fix-eslint": "eslint --ext .vue,.ts,.tsx ./ --max-warnings 0 --ignore-path .gitignore --ignore-path .eslintignore --fix",
Expand Down
55 changes: 52 additions & 3 deletions scripts/test/generate-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,57 @@ const DomParser = require('dom-parser');
const parser = new DomParser();
const result = {};

// 只关注组件本身的测试覆盖率
const components_enum = [
'button',
'fab',
'icon',
'cell',
'divider',
'grid',
'DropdownMenu',
'indexes',
'navbar',
'steps',
'stikcy',
'tabBar',
'checkbox',
'DateTimePicker',
'input',
'picker',
'radio',
'rate',
'search',
'slider',
'stepper',
'switch',
'textarea',
'upload',
'avater',
'badge',
'collapse',
'countDown',
'image',
'imageViewer',
'list',
'reault',
'skeleton',
'swiper',
'tag',
'actionSheet',
'backTop',
'dialog',
'drawer',
'loading',
'message',
'noticeBar',
'overlay',
'popup',
'progress',
'PullDownRefresh',
'swipeCell',
]

function resolveCwd(...args) {
args.unshift(process.cwd());
return path.join(...args);
Expand All @@ -26,17 +77,15 @@ fs.readFile(resolveCwd('test/unit/coverage/index.html'), 'utf8', (err, html) =>

Array.from(tds).forEach((item, index) => {
const col = index % 10;

if (col === 0) {
const [, name] = item.getAttribute('data-value').split('src/');
name && (key = camelCase(name));
components_enum.includes(name) && (key = camelCase(name));
} else if (col === 8) {
value = `${item.getAttribute('data-value')}%`;
} else if (col === 9) {
result[key] = value;
}
});

const finalRes = `module.exports = ${JSON.stringify(result, null, 2)}`;
fs.writeFileSync(resolveCwd('site/web/test-coverage.js'), finalRes);
console.log('successful re-generate coverage');
Expand Down
123 changes: 123 additions & 0 deletions scripts/test/generate-demo-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
const fs = require('fs');
const path = require('path');
const camelCase = require('lodash/camelCase');
const upperFirst = require('lodash/upperFirst');

function resolveCwd(...args) {
args.unshift(process.cwd());
return path.join(...args);
}

const pkg = require(resolveCwd('package.json'));

const framework = pkg.name;

const fixedDateComponentList = ['config-provider', 'time-picker', 'date-picker', 'table', 'form', 'calendar']; // 需要在测试阶段固定日期的组件,table中因为有filter例子 渲染datepicker需要固定

// TODO 过滤掉一些导致挂掉的demo
const filterCom = ['table'];
const filterDemo = {
table: ['virtual-scroll'],
};

const CONFIG = {
'tdesign-mobile-react': {
sourcePath: path.resolve(__dirname, resolveCwd('src')),
targetPath: path.resolve(__dirname, resolveCwd('src')),
defaultTemplate: 'import { mount } from \'@vue/test-utils\';',
},
'tdesign-mobile-vue': {
sourcePath: path.resolve(__dirname, resolveCwd('src')),
targetPath: path.resolve(__dirname, resolveCwd(`src`)),
defaultTemplate: 'import { mount } from \'@vue/test-utils\';',
},
};

const { sourcePath, targetPath, defaultTemplate } = CONFIG[framework];

const data = `/**
* 该文件为由脚本 \`npm run test:demo\` 自动生成,如需修改,执行脚本命令即可。请勿手写直接修改,否则会被覆盖
*/
${defaultTemplate}
{{ HERE IS DEMO LIST }}
`;

function filterFileFunction(files) {
// 过滤非 vue jsx 文件
const fileType = ['vue', 'jsx']
return files.filter(item => {
return fileType.includes(item.substr(item.lastIndexOf('.') + 1, item.length))
})
}

function getKeyFunction(component) {
const newComponent = upperFirst(camelCase(component));

return `
describe('${newComponent}', () => {
Object.keys(mapper).forEach((demoName) => {
it(\`${newComponent} \${demoName} demo works fine\`, () => {
const wrapper = mount(mapper[demoName]);
expect(wrapper.element).toMatchSnapshot();
});
});
});`;
}

function outputOneComponentTestFile(component, demoFiles) {
const outputPath = `${targetPath}/${component}/__test__`;
const imports = [];
const demos = ['\nconst mapper = {'];

demoFiles.forEach((demo) => {
if (filterCom.includes(component) && filterDemo[component].includes(demo.replace('.vue', ''))) return;

const name = camelCase(demo);
imports.push(`import ${name} from '@/${component}/demos/${demo}';`);
demos.push(` ${name},`);
});
if (fixedDateComponentList.includes(component)) {
imports.unshift('import MockDate from \'mockdate\';\n');
imports.push('\nMockDate.set(\'2020-12-28\');');
}

demos.push('};');
const keyData = [imports.join('\n'), demos.join('\n'), getKeyFunction(component)].join('\n');
const testFileData = data.replace('{{ HERE IS DEMO LIST }}', keyData);
fs.mkdir(outputPath, { recursive: true }, (err) => {
if (err) {
console.error(err);
return;
}
fs.writeFile(`${outputPath}/demo.test.tsx`, testFileData, (writeErr) => {
if (writeErr) {
return console.error(writeErr);
}
return console.log(`test file: ${outputPath} has been created.`);
});
});
}

function main() {
fs.readdir(sourcePath, (err, files) => {
if (err) {
console.log('Error', err);
} else {
files.forEach((componentFolder) => {
const demoPath = `${sourcePath}/${componentFolder}/demos`;
fs.readdir(demoPath, (err1, demoFiles) => {
if (err1) {
console.log('Error', err1);
} else {
if (['icon', 'local-provider'].includes(componentFolder)) return;
const finalDemoFiles = filterFileFunction(demoFiles);
outputOneComponentTestFile(componentFolder, finalDemoFiles);
}
});
});
}
});
}

main();
14 changes: 6 additions & 8 deletions site/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,20 @@ const publicPathMap = {

// 单元测试相关配置
const testConfig = {
// include:
// process.env.NODE_ENV === 'test-snap'
// ? ['test/snap/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}']
// : ['test/unit/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
include: ['{test,src}/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
exclude: ['**/ssr/**'],
include:
process.env.NODE_ENV === 'test-snap'
? ['test/snap/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}']
: ['src/**/__test__/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
globals: true,
environment: 'jsdom',
testTimeout: 5000,
setupFiles: path.resolve(__dirname, '../scripts/test/test-setup.js'),
// setupFiles: process.env.NODE_ENV === 'test-snap' ? path.resolve(__dirname, '../scripts/test/test-setup.js') : '',
setupFiles: process.env.NODE_ENV === 'test-snap' ? path.resolve(__dirname, '../scripts/test/test-setup.js') : '',
transformMode: {
web: [/\.[jt]sx$/],
},
coverage: {
reporter: ['text', 'json', 'html'],
reportsDirectory: 'test/unit/coverage',
},
};

Expand Down
153 changes: 2 additions & 151 deletions site/web/test-coverage.js
Original file line number Diff line number Diff line change
@@ -1,153 +1,4 @@
module.exports = {
"": "93.84%",
"actionSheet": "24.48%",
"actionSheet/demos": "0%",
"actionSheet/style": "0%",
"avatar": "91.17%",
"avatarGroup": "35.89%",
"avatarGroup/style": "0%",
"avatar/demos": "0%",
"avatar/style": "0%",
"backTop": "93.93%",
"backTop/demos": "0%",
"backTop/style": "0%",
"badge": "88.57%",
"badge/demos": "0%",
"badge/style": "0%",
"button": "91.17%",
"buttonGroup": "83.33%",
"buttonGroup/style": "0%",
"button/demos": "0%",
"button/style": "0%",
"cell": "100%",
"cellGroup": "92.85%",
"cellGroup/style": "0%",
"cell/demos": "0%",
"cell/style": "0%",
"checkGroup": "13.97%",
"checkGroup/style": "0%",
"checkbox": "35.41%",
"checkbox/demos": "0%",
"checkbox/style": "0%",
"collapse": "27.1%",
"collapse/demos": "0%",
"collapse/style": "0%",
"countDown": "73.68%",
"countDown/demos": "0%",
"countDown/style": "0%",
"dateTimePicker": "12.3%",
"dateTimePicker/demos": "0%",
"dateTimePicker/style": "0%",
"dialog": "24.13%",
"dialog/demos": "0%",
"dialog/style": "0%",
"divider": "70%",
"divider/style": "0%",
"drawer": "42.85%",
"drawer/demos": "0%",
"drawer/style": "0%",
"dropdownMenu": "0%",
"dropdownMenu/demos": "0%",
"dropdownMenu/style": "0%",
"fab": "57.14%",
"fab/style": "0%",
"grid": "41.07%",
"grid/demos": "0%",
"grid/style": "0%",
"image": "31.37%",
"imageViewer": "8.62%",
"imageViewer/demos": "0%",
"imageViewer/style": "0%",
"image/demos": "0%",
"image/style": "0%",
"indexes": "23.89%",
"indexes/demos": "0%",
"indexes/style": "0%",
"input": "29.82%",
"input/demos": "0%",
"input/style": "0%",
"list": "26.49%",
"list/demos": "0%",
"list/style": "0%",
"loading": "39.13%",
"loading/demos": "0%",
"loading/icon": "57.14%",
"loading/style": "0%",
"mask": "76.92%",
"mask/demos": "0%",
"mask/style": "0%",
"message": "32.75%",
"message/demos": "0%",
"message/style": "0%",
"navbar": "50%",
"navbar/demos": "0%",
"navbar/style": "0%",
"noticeBar": "0%",
"noticeBar/demos": "0%",
"noticeBar/style": "0%",
"picker": "12.65%",
"picker/demos": "0%",
"picker/style": "0%",
"popup": "34.88%",
"popup/demos": "0%",
"popup/style": "0%",
"progress": "54.54%",
"progress/style": "0%",
"radio": "32.72%",
"radioGroup": "48%",
"radioGroup/style": "0%",
"radio/demos": "0%",
"radio/style": "0%",
"rate": "30%",
"rate/demos": "0%",
"rate/style": "0%",
"search": "28.94%",
"search/demos": "0%",
"search/style": "0%",
"shared": "66.66%",
"shared/useChildSlots": "10.52%",
"shared/useCountDown": "21.05%",
"shared/useDefault": "7.89%",
"shared/useEmitEvent": "33.33%",
"shared/useInterval": "0%",
"shared/useToggle": "14.28%",
"skeleton": "31.25%",
"skeleton/demos": "0%",
"skeleton/style": "0%",
"slider": "10.28%",
"slider/demos": "0%",
"slider/style": "0%",
"stepper": "35.55%",
"stepper/demos": "0%",
"stepper/style": "0%",
"steps": "32.46%",
"steps/demos": "0%",
"steps/style": "0%",
"sticky": "29.78%",
"sticky/demos": "0%",
"sticky/style": "0%",
"style": "0%",
"swipeCell": "15.38%",
"swipeCell/demos": "0%",
"swipeCell/style": "0%",
"swiper": "14.28%",
"swiper/demos": "0%",
"swiper/style": "0%",
"switch": "51.72%",
"switch/demos": "0%",
"switch/style": "0%",
"tabBar": "40.9%",
"tabBar/demos": "0%",
"tabBar/style": "0%",
"tabs": "29%",
"tabs/demos": "0%",
"tabs/style": "0%",
"tag": "39.7%",
"tag/demos": "0%",
"tag/style": "0%",
"textarea": "23.07%",
"textarea/style": "0%",
"toast": "34.32%",
"toast/demos": "0%",
"toast/style": "0%"
"": "100%",
"checkbox": "17.64%"
}
Loading

0 comments on commit b21b586

Please sign in to comment.