Skip to content

Commit 68a822b

Browse files
author
rhys williams
committed
Make the tests more robust by using extractSummary method to compare
stderr snapshots. Do a replace on the stderr which has a full path to a failed glob threshold file which won't match when the test runs on windows (appveyor)
1 parent 3c36107 commit 68a822b

2 files changed

Lines changed: 53 additions & 27 deletions

File tree

e2e/__tests__/__snapshots__/coverage_threshold.test.js.snap

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`excludes tests matched by path threshold groups from global group: stderr 1`] = `
3+
exports[`excludes tests matched by path threshold groups from global group 1`] = `
44
"PASS __tests__/banana.test.js
5-
✓ banana (<<REPLACED>>)
5+
✓ banana
66
77
Jest: \\"global\\" coverage threshold for lines (100%) not met: 0%
8-
Test Suites: 1 passed, 1 total
8+
"
9+
`;
10+
11+
exports[`excludes tests matched by path threshold groups from global group 2`] = `
12+
"Test Suites: 1 passed, 1 total
913
Tests: 1 passed, 1 total
1014
Snapshots: 0 total
1115
Time: <<REPLACED>>
@@ -33,12 +37,16 @@ All files | 100 | 100 | 100 | 100 | |
3337
-----------|----------|----------|----------|----------|-------------------|"
3438
`;
3539
36-
exports[`exits with 1 if coverage threshold is not met: stderr 1`] = `
40+
exports[`exits with 1 if coverage threshold is not met 1`] = `
3741
"PASS __tests__/a-banana.js
38-
✓ banana (<<REPLACED>>)
42+
✓ banana
3943
4044
Jest: \\"global\\" coverage threshold for lines (90%) not met: 50%
41-
Test Suites: 1 passed, 1 total
45+
"
46+
`;
47+
48+
exports[`exits with 1 if coverage threshold is not met 2`] = `
49+
"Test Suites: 1 passed, 1 total
4250
Tests: 1 passed, 1 total
4351
Snapshots: 0 total
4452
Time: <<REPLACED>>
@@ -56,12 +64,16 @@ All files | 50 | 100 | 0 | 50 |
5664
"
5765
`;
5866
59-
exports[`exits with 1 if path threshold group is not found in coverage data: stderr 1`] = `
67+
exports[`exits with 1 if path threshold group is not found in coverage data 1`] = `
6068
"PASS __tests__/banana.test.js
61-
✓ banana (<<REPLACED>>)
69+
✓ banana
6270
6371
Jest: Coverage data for apple.js was not found.
64-
Test Suites: 1 passed, 1 total
72+
"
73+
`;
74+
75+
exports[`exits with 1 if path threshold group is not found in coverage data 2`] = `
76+
"Test Suites: 1 passed, 1 total
6577
Tests: 1 passed, 1 total
6678
Snapshots: 0 total
6779
Time: <<REPLACED>>
@@ -79,14 +91,18 @@ All files | 100 | 100 | 100 | 100 | |
7991
"
8092
`;
8193
82-
exports[`file is matched by all path and glob threshold groups: stderr 1`] = `
94+
exports[`file is matched by all path and glob threshold groups 1`] = `
8395
"PASS __tests__/banana.test.js
84-
✓ banana (<<REPLACED>>)
96+
✓ banana
8597
8698
Jest: \\"./\\" coverage threshold for lines (100%) not met: 50%
87-
Jest: \\"<<REPLACED>>/e2e/coverage-threshold/banana.js\\" coverage threshold for lines (100%) not met: 50%
88-
Jest: \\"banana.js\\" coverage threshold for lines (100%) not met: 50%
89-
Test Suites: 1 passed, 1 total
99+
Jest: \\"<<FULL_PATH_TO_BANANA_JS>>\\" coverage threshold for lines (100%) not met: 50%
100+
Jest: \\"./banana.js\\" coverage threshold for lines (100%) not met: 50%
101+
"
102+
`;
103+
104+
exports[`file is matched by all path and glob threshold groups 2`] = `
105+
"Test Suites: 1 passed, 1 total
90106
Tests: 1 passed, 1 total
91107
Snapshots: 0 total
92108
Time: <<REPLACED>>

e2e/__tests__/coverage_threshold.test.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,14 @@
1010
'use strict';
1111

1212
const path = require('path');
13-
const {cleanup, writeFiles} = require('../Utils');
13+
const {cleanup, writeFiles, extractSummary} = require('../Utils');
1414
const runJest = require('../runJest');
1515

1616
const DIR = path.resolve(__dirname, '../coverage-threshold');
1717

1818
beforeEach(() => cleanup(DIR));
1919
afterAll(() => cleanup(DIR));
2020

21-
const replaceTime = str => {
22-
return str.replace(/\d*\.?\d+m?s/g, '<<REPLACED>>');
23-
};
24-
2521
test('exits with 1 if coverage threshold is not met', () => {
2622
const pkgJson = {
2723
jest: {
@@ -49,9 +45,12 @@ test('exits with 1 if coverage threshold is not met', () => {
4945
});
5046

5147
const {stdout, stderr, status} = runJest(DIR, ['--coverage', '--ci=false']);
48+
const {rest, summary} = extractSummary(stderr);
49+
5250
expect(status).toBe(1);
51+
expect(rest).toMatchSnapshot();
52+
expect(summary).toMatchSnapshot();
5353
expect(stdout).toMatchSnapshot('stdout');
54-
expect(replaceTime(stderr)).toMatchSnapshot('stderr');
5554
});
5655

5756
test('exits with 1 if path threshold group is not found in coverage data', () => {
@@ -81,10 +80,12 @@ test('exits with 1 if path threshold group is not found in coverage data', () =>
8180
});
8281

8382
const {stdout, stderr, status} = runJest(DIR, ['--coverage', '--ci=false']);
83+
const {rest, summary} = extractSummary(stderr);
8484

8585
expect(status).toBe(1);
86+
expect(rest).toMatchSnapshot();
87+
expect(summary).toMatchSnapshot();
8688
expect(stdout).toMatchSnapshot('stdout');
87-
expect(replaceTime(stderr)).toMatchSnapshot('stderr');
8889
});
8990

9091
test('exits with 0 if global threshold group is not found in coverage data', () => {
@@ -157,10 +158,12 @@ test('excludes tests matched by path threshold groups from global group', () =>
157158
});
158159

159160
const {stdout, stderr, status} = runJest(DIR, ['--coverage', '--ci=false']);
161+
const {rest, summary} = extractSummary(stderr);
160162

161163
expect(status).toBe(1);
164+
expect(rest).toMatchSnapshot();
165+
expect(summary).toMatchSnapshot();
162166
expect(stdout).toMatchSnapshot('stdout');
163-
expect(replaceTime(stderr)).toMatchSnapshot('stderr');
164167
});
165168

166169
test('file is matched by all path and glob threshold groups', () => {
@@ -172,10 +175,10 @@ test('file is matched by all path and glob threshold groups', () => {
172175
'./': {
173176
lines: 100,
174177
},
175-
'ban*.js': {
178+
'./ban*.js': {
176179
lines: 100,
177180
},
178-
'banana.js': {
181+
'./banana.js': {
179182
lines: 100,
180183
},
181184
},
@@ -196,10 +199,17 @@ test('file is matched by all path and glob threshold groups', () => {
196199
});
197200

198201
const {stdout, stderr, status} = runJest(DIR, ['--coverage', '--ci=false']);
202+
const {rest, summary} = extractSummary(
203+
/* This test also runs on windows and when the glob fails it outputs
204+
the system specific absolute path to the test file. */
205+
stderr.replace(
206+
path.resolve(DIR, './banana.js'),
207+
'<<FULL_PATH_TO_BANANA_JS>>',
208+
),
209+
);
199210

200211
expect(status).toBe(1);
212+
expect(rest).toMatchSnapshot();
213+
expect(summary).toMatchSnapshot();
201214
expect(stdout).toMatchSnapshot('stdout');
202-
expect(
203-
replaceTime(stderr).replace(process.cwd(), '<<REPLACED>>'),
204-
).toMatchSnapshot('stderr');
205215
});

0 commit comments

Comments
 (0)