How to use getCoverageOptions method in Jest

Best JavaScript code snippet using jest

Run Jest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

index.js

Source: index.js Github

copy
1import path from 'path';
2import { argv, paths } from '../config';
3import resolve from '../config/resolve';
4
5import webpack from '../webpack/test/client';
6import webpackMiddleware from '../webpack/development/devMiddleware';
7
8import plugins from './plugins';
9import frameworks from './frameworks';
10import customLaunchers from './launchers';
11import getCoverageOptions from './coverage';
12
13// use Chrome launcher only on Travis CI
14const browser = process.env.CI && process.env.TRAVIS_CI ?
15  'ChromeTravisCI' : 'PhantomJS';
16
17const testsPath = resolve.test('index.js');
18
19const karmaConfig = {
20  files: [
21    './node_modules/phantomjs-polyfill/bind-polyfill.js',
22    testsPath
23  ],
24  frameworks,
25  preprocessors: {
26    [testsPath]: ['webpack', 'sourcemap']
27  },
28  reporters: ['dots'],
29  browsers: [browser],
30  customLaunchers,
31  webpack,
32  webpackMiddleware,
33  plugins,
34  singleRun: !argv.watch,
35  autoWatch: true,
36  autoWatchBatchDelay: 0,
37  reportSlowerThan: 40,
38  concurrency: 2,
39  browserNoActivityTimeout: 30000,
40};
41
42const coverage = getCoverageOptions(argv);
43
44if (coverage.enabled) {
45  const srcWildcard = resolve.src('**/*.js');
46
47  karmaConfig.preprocessors[srcWildcard] = ['coverage'];
48  karmaConfig.reporters.push('coverage');
49  karmaConfig.coverageReporter = { reporters: coverage.reporters };
50
51  karmaConfig.webpack.module.preLoaders = [
52    { test: /\.jsx?$/, include: paths.scripts, loader: 'isparta' }
53  ];
54}
55
56export default karmaConfig;
57
Full Screen

jest.config.js

Source: jest.config.js Github

copy
1const getCoverageOptions = () => {
2    return process.env.COVERAGE === "include"
3        ? {
4            collectCoverage: true,
5            testMatch: ["**/*.test.tsx", "**/*.test.ts", "**/*.test.js"],
6            coverageThreshold: {
7                global: {
8                    branches: 40,
9                    functions: 40,
10                    lines: 40,
11                    statements: 40,
12                },
13            },
14            coverageDirectory: "./coverage",
15        }
16        : {
17            collectCoverage: false,
18        };
19};
20
21module.exports = {
22    ...getCoverageOptions(),
23    testMatch: ["**/*.test.tsx", "**/*.test.ts", "**/*.test.js"],
24    setupFilesAfterEnv: ["<rootDir>src/setupTests.tsx"],
25    coveragePathIgnorePatterns: [
26        ".*\\.mock.ts$",
27        ".*\\.module.ts$",
28        ".*\\.config.ts$",
29        ".*\\.models.ts$",
30        "setupTests.tsx",
31        "index.tsx",
32        "index.ts",
33        "index.tsx",
34        "/environments/",
35        "/utils/",
36        "/assets/",
37        "/icons/",
38    ],
39};
40
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Jest on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)