-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Expand file tree
/
Copy pathcoverage_report.test.js
More file actions
103 lines (88 loc) · 3.02 KB
/
coverage_report.test.js
File metadata and controls
103 lines (88 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
'use strict';
const fs = require('fs');
const path = require('path');
const skipOnWindows = require('../../scripts/skip_on_windows');
const {extractSummary} = require('../utils');
const runJest = require('../runJest');
const DIR = path.resolve(__dirname, '../coverage_report');
skipOnWindows.suite();
test('outputs coverage report', () => {
const {stdout, status} = runJest(DIR, ['--no-cache', '--coverage']);
const coverageDir = path.resolve(__dirname, '../coverage_report/coverage');
// - the `setup.js` file is ignored and should not be in the coverage report.
// - `sum_dependency.js` is mocked and the real module is never required but
// is listed with 0 % coverage.
// - `not-required-in-test-suite.js` is not required but it is listed
// with 0 % coverage.
expect(stdout).toMatchSnapshot();
expect(() => fs.accessSync(coverageDir, fs.F_OK)).not.toThrow();
expect(status).toBe(0);
});
test('collects coverage only from specified files', () => {
const {stdout} = runJest(DIR, [
'--no-cache',
'--coverage',
'--collectCoverageFrom', // overwrites the one in package.json
'setup.js',
]);
// Coverage report should only have `setup.js` coverage info
expect(stdout).toMatchSnapshot();
});
test('collects coverage only from specified files avoiding dependencies', () => {
const {stdout} = runJest(DIR, [
'--no-cache',
'--coverage',
'--collectCoverageOnlyFrom',
'sum.js',
'--',
'sum.test.js',
]);
// Coverage report should only have `sum.js` coverage info
expect(stdout).toMatchSnapshot();
});
test('json reporter printing with --coverage', () => {
const {stderr, status} = runJest('json_reporter', ['--coverage']);
const {summary} = extractSummary(stderr);
expect(status).toBe(1);
expect(summary).toMatchSnapshot();
});
test('outputs coverage report as json', () => {
const {stdout, status} = runJest(DIR, ['--no-cache', '--coverage', '--json']);
expect(status).toBe(0);
try {
JSON.parse(stdout);
} catch (err) {
throw new Error(
"Can't parse the JSON result from stdout. " + err.toString(),
);
}
});
test('collects coverage from duplicate files avoiding shared cache', () => {
const args = [
'--coverage',
// Ensure the status code is non-zero if super edge case with coverage triggers
'--coverageThreshold',
'{"global": {"lines": 100}}',
'--collectCoverageOnlyFrom',
'cached-duplicates/a/identical.js',
'--collectCoverageOnlyFrom',
'cached-duplicates/b/identical.js',
'--',
'identical.test.js',
];
// Run once to prime the cache
runJest(DIR, args);
// Run for the second time
const {stdout, status} = runJest(DIR, args);
expect(stdout).toMatchSnapshot();
expect(status).toBe(0);
});