Jest Config:
module.exports = {
setupFiles: [],
clearMocks: true,
collectCoverage: true,
testEnvironment: 'jsdom',
transform: {
'^.+.tsx?$': ['ts-jest', { diagnostics: false, babelConfig: true }],
'^.+.js$': 'babel-jest',
'node_modules/vue-runtime-helpers/.+.mjs$': 'babel-jest',
'..(vue)$': '@vue/vue2-jest'
},
roots: ['/tests'],
testMatch: ['**/.test.ts'],
coverageThreshold: {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: 80
}
},
// 1:解决Jest: a transform must export a process or processAsync function.
moduleNameMapper: {
'^.+.(css|less|scss)$': '/jest/style.transform.js',
'^@/(.)$': '/src/$1',
'^@shared(.)$': '/shared$1',
'^@components(.)$': '/src/components$1',
'^@models(.)$': '/src/models$1',
'^@utils(.)$': '/src/utils$1',
'^@configs(.)$': '/src/configs$1',
'^@framework(.)$': '/src/framework$1',
appConfig: <rootDir>/src/configs/globalConfigs/development.config.js,
'mihoyo-game-selector': '@mihoyo-fe/mihoyo-ui/es/components/select',
'.(jpg|ico|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
'/src/test/fileMock.js'
},
transformIgnorePatterns: ['node_modules/(?!vue-runtime-helpers|@mihoyo-fe/mihoyo-ui|element-ui|history)'],
// globals: {
// 'ts-jest': {
// tsConfig: './tsconfig.json'
// }
// },
// testRegex: '(/tests/.|(.|/)(test|spec)).tsx?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node', 'vue']
};
Test Code:
export const getData = (a: number, b: number) => {
if (a === 1) {
console.log(2);
return 3;
}
if (a > 1) {
console.log(1);
return 2;
}
return 1;
};
Test Case:
describe('getData', () => {
it('getData测试', () => {
expect(getData(1, 2)).toBe(3);
});
});
I am using the jest tool to write the above case and test the relevant code. The result is that the branch coverage is 100%, but I think it should be 50% because if (a > 1) {} Branch, unit test case is not covered
branch coverage is 50%
Why is the above branch coverage not 50%, but 100%? because i see If (a > 1) {} is not executed
shuliangShine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.