我已经用 jest 配置了 lint-staged:
"lint-staged": {
"src/**/*.{ts,tsx}": ["jest --bail --coverage --findRelatedTests"],
},
我还有以下jest.config.js
配置文件:
const nextJest = require('next/jest');
const createJestConfig = nextJest({
dir: './',
});
const customJestConfig = {
collectCoverageFrom: ['src/**/*.{ts,tsx}', '!src/**/*.d.ts'],
coveragePathIgnorePatterns: ['<rootDir>/src/pages/', '<rootDir>/src/i18n/', '<rootDir>/src/icons.ts'],
coverageThreshold: {
global: {
branches: 90,
functions: 90,
lines: 90,
statements: 90,
},
},
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
moduleNameMapper: {
'^@/containers/(.*)$': '<rootDir>/src/components/containers/$1',
'^@/layout/(.*)$': '<rootDir>/src/components/layout/$1',
'^@/ui/(.*)$': '<rootDir>/src/components/ui/$1',
'^@/utils/(.*)$': '<rootDir>/src/utils/$1',
'^@/images/(.*)$': '<rootDir>/public/images/$1',
'^@/models/(.*)$': '<rootDir>/src/models/$1',
},
moduleDirectories: ['node_modules', '<rootDir>/'],
testEnvironment: 'jest-environment-jsdom',
};
module.exports = createJestConfig(customJestConfig);
据我所知,像我在 lint-staged 阶段尝试的方式运行 jest 应该失败,原因有两个:1. 在 lint-staged 阶段检查的文件没有测试文件 2. 覆盖率应该因为 0% 结果而失败。我已将其配置为阈值 90%。
但是,当运行git commit ...
lint-staged 开始时,我得到:
[STARTED] jest --bail --coverage --findRelatedTests
[SUCCESS] jest --bail --coverage --findRelatedTests
它没有失败。为什么?