Jest 覆盖工具失败
Jest coverage tools fail
在我的 react-native 项目中,在测试执行期间,Jest 显示覆盖率并创建覆盖率报告。
Jest 配置:
import type {Config} from '@jest/types';
const config: Config.InitialOptions = {
// basic params to setup test ext and env
preset: '@testing-library/react-native',
verbose: true,
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
roots: [
'<rootDir>'
],
// tests coverage
collectCoverage: true,
coverageDirectory: "__tests__/coverage",
coverageReporters: [
'lcov',
'text',
],
collectCoverageFrom: [
"**/src/**/*.{js,jsx,ts,tsx}",
"!**/src/parameters/**/*.{js,jsx,ts,tsx}",
"!**/src/types/**/*.{js,jsx,ts,tsx}",
"!**/src/navigationRoots/**/*.{js,jsx,ts,tsx}",
"!**/node_modules/**",
],
coverageThreshold: {
global: {
lines: 70,
statements: 70
}
},
// additional
testRegex: "(/__tests__/.*|\.(test|spec))\.(ts|tsx|js)$",
transform: {
'^.+\.(js|ts|tsx)$': 'babel-jest'
},
transformIgnorePatterns: [
"node_modules/(?!(jest-)?@react-native|react-native|react-(native|universal|navigation)-(.*)" +
"|@react-native-community/(.*)" +
"|@react-navigation/(.*)" +
"|bs-platform" +
"|(@[a-zA-Z]+/)?(bs|reason|rescript)-(.*)+)"
],
};
export default config;
在测试过程中出现错误:
Consider using the "jsdom" test environment.
ReferenceError: document is not defined
Consider using the "jsdom" test environment.
ReferenceError: window is not defined
在创建覆盖文件夹期间生成的文件中:
coverage/lcov-report/sorter.js
coverage/lcov-report/block-navigation.js
因此,在 jest documentation 中我们看到我们可以在文件中指定 jsdom 环境,这会产生如下错误:
/**
* @jest-environment jsdom
*/
好的,但是这里有自动生成的文件,不是我的测试文件。我还能如何解决这些错误?
UPD:如果我在启动前删除包含所有文件的覆盖文件夹,则不会出现这些错误 tests.so Jest 创造一切美好。但是当我使用现有的“coverage”文件夹启动测试时,更新期间显示错误
找到了我的问题的答案。当您将 jest tests 文件夹指定为文件夹以将覆盖率报告放在那里时,Jest 认为,该覆盖率文件夹包含 test.on 首先在覆盖率文件夹不存在时启动,它会毫无问题地创建它,但是当您重复覆盖率命令时,jest 会尝试测试其中的每个js文件。因此需要将 coverage 文件夹排除为 jest 的测试位置。如何做到这一点你可以找到
在我的 react-native 项目中,在测试执行期间,Jest 显示覆盖率并创建覆盖率报告。
Jest 配置:
import type {Config} from '@jest/types';
const config: Config.InitialOptions = {
// basic params to setup test ext and env
preset: '@testing-library/react-native',
verbose: true,
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
roots: [
'<rootDir>'
],
// tests coverage
collectCoverage: true,
coverageDirectory: "__tests__/coverage",
coverageReporters: [
'lcov',
'text',
],
collectCoverageFrom: [
"**/src/**/*.{js,jsx,ts,tsx}",
"!**/src/parameters/**/*.{js,jsx,ts,tsx}",
"!**/src/types/**/*.{js,jsx,ts,tsx}",
"!**/src/navigationRoots/**/*.{js,jsx,ts,tsx}",
"!**/node_modules/**",
],
coverageThreshold: {
global: {
lines: 70,
statements: 70
}
},
// additional
testRegex: "(/__tests__/.*|\.(test|spec))\.(ts|tsx|js)$",
transform: {
'^.+\.(js|ts|tsx)$': 'babel-jest'
},
transformIgnorePatterns: [
"node_modules/(?!(jest-)?@react-native|react-native|react-(native|universal|navigation)-(.*)" +
"|@react-native-community/(.*)" +
"|@react-navigation/(.*)" +
"|bs-platform" +
"|(@[a-zA-Z]+/)?(bs|reason|rescript)-(.*)+)"
],
};
export default config;
在测试过程中出现错误:
Consider using the "jsdom" test environment.
ReferenceError: document is not defined
Consider using the "jsdom" test environment.
ReferenceError: window is not defined
在创建覆盖文件夹期间生成的文件中:
coverage/lcov-report/sorter.js
coverage/lcov-report/block-navigation.js
因此,在 jest documentation 中我们看到我们可以在文件中指定 jsdom 环境,这会产生如下错误:
/**
* @jest-environment jsdom
*/
好的,但是这里有自动生成的文件,不是我的测试文件。我还能如何解决这些错误?
UPD:如果我在启动前删除包含所有文件的覆盖文件夹,则不会出现这些错误 tests.so Jest 创造一切美好。但是当我使用现有的“coverage”文件夹启动测试时,更新期间显示错误
找到了我的问题的答案。当您将 jest tests 文件夹指定为文件夹以将覆盖率报告放在那里时,Jest 认为,该覆盖率文件夹包含 test.on 首先在覆盖率文件夹不存在时启动,它会毫无问题地创建它,但是当您重复覆盖率命令时,jest 会尝试测试其中的每个js文件。因此需要将 coverage 文件夹排除为 jest 的测试位置。如何做到这一点你可以找到