jest 学习笔记

594 阅读1分钟

  • 基础配置

 mapCoverage: false 运行报错 需要删除;

 rootDir: path.resolve(__dirname, '../../'), 定义根路径
 moduleNameMapper: {
  // 支持源代码中相同的 `@` -> `src` 别名
  '^@/(.*)$': '<rootDir>/src/$1'
},
// 需要忽略的测试文件
testPathIgnorePatterns: [
  '<rootDir>/test/e2e',
  '<rootDir>/test/unit'
],
transform: {
  // 用 `babel-jest` 处理 js  为了可以用es6语法
  '^.+\\.js$': '<rootDir>/node_modules/babel-jest',
  // 用 `vue-jest` 处理 `*.vue` 文件
  '.*\\.(vue)$': '<rootDir>/node_modules/vue-jest'
},
testRegex:'(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$' 
默认情况下,Jest 将会递归的找到整个工程里所有 .spec.js 或 .test.js 扩展名的文件 ,可根据需要修改
  • 报错集合
testURL: 'http://localhost',  不设置会报‘localstorage is not available for opaque origins’