jest+enzyme 组件测试配置
2020-04-14 本文已影响0人
glory_前端
1.安装依赖jest,enzyme,enzyme-adapter-react-16,enzyme-to-json
2.在根目录新建.jest.js配置文件,根据文档添加扩展
module.exports = {
roots: ['<rootDir>/__test__/', '<rootDir>/src/'], // 测试的目录
modulePaths: ['<rootDir>'],
coveragePathIgnorePatterns: ['/node_modules/'], // 忽略统计覆盖率的文件
// transform: {
// '^.+\\.js$': 'babel-jest',
// '^.+\\.(ts|tsx)$': 'ts-jest',
// },
transformIgnorePatterns: [
'<rootDir>/node_modules/(?!(lodash-es|other-es-lib))',
], //
testRegex: '(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$',
moduleNameMapper: {
'^.+\\.(css|sass|scss)$': 'identity-obj-proxy',
}, // 代表需要被Mock的资源名称
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], //支持文件名
};
3.在根目录下面创建test文件

4.package.json
script{
"test": "jest --coverage --config .jest.js"
}
5.新建测试文档


6 npm run test 或者npm test<name>
