/
customReporters.test.ts
188 lines (158 loc) · 4.91 KB
/
customReporters.test.ts
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {tmpdir} from 'os';
import * as path from 'path';
import {wrap} from 'jest-snapshot-serializer-raw';
import {onNodeVersions} from '@jest/test-utils';
import {cleanup, extractSummary, writeFiles} from '../Utils';
import runJest from '../runJest';
const DIR = path.resolve(tmpdir(), 'custom-reporters-test-dir');
beforeEach(() => cleanup(DIR));
afterEach(() => cleanup(DIR));
describe('Custom Reporters Integration', () => {
test('valid string format for adding reporters', () => {
const reporterConfig = {
reporters: ['<rootDir>/reporters/TestReporter.js'],
};
const {exitCode} = runJest('custom-reporters', [
'--config',
JSON.stringify(reporterConfig),
'add.test.js',
]);
expect(exitCode).toBe(0);
});
test('valid array format for adding reporters', () => {
const reporterConfig = {
reporters: [
['<rootDir>/reporters/TestReporter.js', {'Aaron Abramov': 'Awesome'}],
],
};
const {exitCode, stdout} = runJest('custom-reporters', [
'--config',
JSON.stringify(reporterConfig),
'add.test.js',
]);
expect(wrap(stdout)).toMatchSnapshot();
expect(exitCode).toBe(0);
});
test('invalid format for adding reporters', () => {
const reporterConfig = {
reporters: [[3243242]],
};
const {exitCode, stderr} = runJest('custom-reporters', [
'--config',
JSON.stringify(reporterConfig),
'add.test.js',
]);
expect(exitCode).toBe(1);
expect(wrap(stderr)).toMatchSnapshot();
});
test('default reporters enabled', () => {
const {stderr, stdout, exitCode} = runJest('custom-reporters', [
'--config',
JSON.stringify({
reporters: ['default', '<rootDir>/reporters/TestReporter.js'],
}),
'add.test.js',
]);
const {summary, rest} = extractSummary(stderr);
const parsedJSON = JSON.parse(stdout);
expect(exitCode).toBe(0);
expect(wrap(rest)).toMatchSnapshot();
expect(wrap(summary)).toMatchSnapshot();
expect(parsedJSON).toMatchSnapshot();
});
test('TestReporter with all tests passing', () => {
const {stdout, exitCode, stderr} = runJest('custom-reporters', [
'add.test.js',
]);
const parsedJSON = JSON.parse(stdout);
expect(exitCode).toBe(0);
expect(stderr).toBe('');
expect(parsedJSON).toMatchSnapshot();
});
test('TestReporter with all tests failing', () => {
const {stdout, exitCode, stderr} = runJest('custom-reporters', [
'addFail.test.js',
]);
const parsedJSON = JSON.parse(stdout);
expect(exitCode).toBe(1);
expect(stderr).toBe('');
expect(parsedJSON).toMatchSnapshot();
});
test('IncompleteReporter for flexibility', () => {
const {stderr, stdout, exitCode} = runJest('custom-reporters', [
'--no-cache',
'--config',
JSON.stringify({
reporters: ['<rootDir>/reporters/IncompleteReporter.js'],
}),
'add.test.js',
]);
expect(exitCode).toBe(0);
expect(stderr).toBe('');
expect(wrap(stdout)).toMatchSnapshot();
});
test('reporters can be default exports', () => {
const {stderr, stdout, exitCode} = runJest('custom-reporters', [
'--no-cache',
'--config',
JSON.stringify({
reporters: ['<rootDir>/reporters/DefaultExportReporter.js'],
}),
'add.test.js',
]);
expect(stderr).toBe('');
expect(exitCode).toBe(0);
expect(wrap(stdout)).toMatchSnapshot();
});
test('prints reporter errors', () => {
writeFiles(DIR, {
'__tests__/test.test.js': `test('test', () => {});`,
'package.json': JSON.stringify({
jest: {
reporters: ['default', '<rootDir>/reporter.js'],
testEnvironment: 'node',
},
}),
'reporter.js': `
'use strict';
module.exports = class Reporter {
onRunStart() {
throw new Error('ON_RUN_START_ERROR');
}
};
`,
});
const {stderr, exitCode} = runJest(DIR);
expect(stderr).toMatch(/ON_RUN_START_ERROR/);
expect(exitCode).toBe(1);
});
onNodeVersions('^12.17.0 || >=13.2.0', () => {
test('supports reporter written in ESM', () => {
writeFiles(DIR, {
'__tests__/test.test.js': `test('test', () => {});`,
'package.json': JSON.stringify({
jest: {
reporters: ['default', '<rootDir>/reporter.mjs'],
testEnvironment: 'node',
},
}),
'reporter.mjs': `
export default class Reporter {
onRunStart() {
throw new Error('ON_RUN_START_ERROR');
}
};
`,
});
const {stderr, exitCode} = runJest(DIR);
expect(stderr).toMatch(/ON_RUN_START_ERROR/);
expect(exitCode).toBe(1);
});
});
});