Open jerryni opened 6 years ago
module.exports = {
'moduleFileExtensions': [
'js',
'vue'
],
'moduleDirectories': [
'node_modules',
'packages'
],
'moduleNameMapper': { // 相当于webpack里的alias
'^packages/(.*)$': '<rootDir>/packages/$1',
'^docs/(.*)$': '<rootDir>/docs/examples-docs/$1',
'^examples-dist/(.*)$': '<rootDir>/docs/examples-dist/$1',
'^test/(.*)$': '<rootDir>/test/$1'
},
'transform': { // 相当于webpack里的loader
'^.+\\.js$': '<rootDir>/node_modules/babel-jest',
'.*\\.(vue)$': '<rootDir>/node_modules/vue-jest',
'.+\\.(css|styl|less|sass|scss)$': 'jest-transform-css'
},
'transformIgnorePatterns': [ // 不转化的东西,这里可以排除掉转化的库,比如vant
'/node_modules/(?!vant)'
],
'snapshotSerializers': [
'<rootDir>/node_modules/jest-serializer-vue'
],
'collectCoverageFrom': [ // 代码覆盖率考虑的文件
'packages/**/*.{js,vue}',
'!**/lib/**',
'!**/ajax/**'
],
'coverageDirectory': 'test/unit/coverage'
};
hello world
在线 demo: https://github.com/facebook/jest.git
jest/examples/snapshot里的目录结构:
实例
运行下面的测试代码:
将生成如下的snapshot文件:
jest --updateSnapshot
或简写jest -u
; 可以通过jest --testNamePattern=<regex>;
指定匹配模块; 如jest -t=anchor.*page
, anchor.*page可以匹配到'renders as an anchor when no page is set'
这个模块;最佳实践
Date.now = jest.fn(() => 1482363367071);
exports[
<UserName /> should render Alan Turing
] = `