Skip to content
Advertisement

Jest coverage tools fail

During in my react-native project, during test execution, Jest shows coverage and creates coverage reports.

Jest config:

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;

During testing I get errors:

Consider using the "jsdom" test environment.
    
    ReferenceError: document is not defined



Consider using the "jsdom" test environment.
    
    ReferenceError: window is not defined

in files which are generated during creation of coverage folder:

coverage/lcov-report/sorter.js

coverage/lcov-report/block-navigation.js

So, in jest documentation we see that we can specify jsdom environment in file which produces error like:

/**
 * @jest-environment jsdom
 */

Ok, but here we have auto-generated files, not my test-files. How else can I fix these errors?

UPD: these errors does not appear if I delete coverage folder with all files before launching tests.so Jest creates everything good. But when I launch tests with existing “coverage” folder, during update shows errors

Advertisement

Answer

found answer on my question. when you specify jest tests folder as folder to place coverage report there, Jest considers, that coverage folder contains test.on first start when coverage folder does not exist it creates it without problems, but when you repeat coverage command, jest tries to test every js file in it. so coverage folder is needed to be excluded as test location for jest. how to do this you can find here

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement