[Bug]: jest branch coverage seems to be misunderstood by if statement #15428
Description
Version
29.7.0
Steps to reproduce
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; // 返回值改变为2以能通过更好的测试
}
if (a > 1) {
console.log(1);
return 2;
}
return 1;
};
Test Case:
describe('getData', () => {
it('getData测试', () => {
expect(getData(1, 2)).toBe(3);
});
});
Expected behavior
branch coverage is 50%
Actual behavior
branch coverage is 100%
Why is the above branch coverage not 50%, but 100%? because i see If (a > 1) {} is not executed
Additional context
No response
Environment
System:
OS: macOS 12.1
CPU: (10) arm64 Apple M1 Pro
Binaries:
Node: 20.17.0 - ~/.nvm/versions/node/v20.17.0/bin/node
Yarn: 1.22.21 - /usr/local/bin/yarn
npm: 10.8.2 - ~/.nvm/versions/node/v20.17.0/bin/npm
pnpm: 9.12.3 - ~/.nvm/versions/node/v20.17.0/bin/pnpm
npmPackages:
jest: ^29.7.0 => 29.7.0