美文网首页
jest+enzyme 组件测试配置

jest+enzyme 组件测试配置

作者: glory_前端 | 来源:发表于2020-04-14 17:41 被阅读0次
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文件
image.png
4.package.json
script{
 "test": "jest --coverage --config .jest.js"
}
5.新建测试文档
image.png
image.png
6 npm run test 或者npm test<name>
image.png

相关文章

网友评论

      本文标题:jest+enzyme 组件测试配置

      本文链接:https://www.haomeiwen.com/subject/tqwtvhtx.html