Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: jest branch coverage seems to be misunderstood by if statement #15428

Open
yizhengfeng-jj opened this issue Dec 26, 2024 · 0 comments
Open

Comments

@yizhengfeng-jj
Copy link

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant