Skip to content

Commit 983246d

Browse files
aaronabramovcpojer
authored andcommitted
custom reporter error handling (jestjs#4051)
1 parent 0180b81 commit 983246d

File tree

5 files changed

+69
-36
lines changed

5 files changed

+69
-36
lines changed

integration_tests/__tests__/custom_reporters.test.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@
88
'use strict';
99

1010
const skipOnWindows = require('skipOnWindows');
11-
const {extractSummary} = require('../utils');
11+
const {cleanup, extractSummary, writeFiles} = require('../utils');
1212
const runJest = require('../runJest');
13+
const os = require('os');
14+
const path = require('path');
15+
16+
const DIR = path.resolve(os.tmpdir(), 'custom_reporters_test_dir');
17+
18+
beforeEach(() => cleanup(DIR));
19+
afterEach(() => cleanup(DIR));
1320

1421
describe('Custom Reporters Integration', () => {
1522
skipOnWindows.suite();
@@ -120,4 +127,27 @@ describe('Custom Reporters Integration', () => {
120127

121128
expect(stdout).toMatchSnapshot();
122129
});
130+
131+
test('prints reporter errors', () => {
132+
writeFiles(DIR, {
133+
'__tests__/test.test.js': `test('test', () => {});`,
134+
'package.json': JSON.stringify({
135+
jest: {
136+
testEnvironment: 'node',
137+
reporters: ['default', '<rootDir>/reporter.js'],
138+
},
139+
}),
140+
'reporter.js': `
141+
module.exports = class Reporter {
142+
onRunStart() {
143+
throw new Error('ON_RUN_START_ERROR');
144+
}
145+
};
146+
`,
147+
});
148+
149+
const {stderr, status} = runJest(DIR);
150+
expect(stderr).toMatch(/ON_RUN_START_ERROR/);
151+
expect(status).toBe(1);
152+
});
123153
});

packages/jest-cli/src/cli/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ const _run = async (
258258
);
259259

260260
globalConfig.watch || globalConfig.watchAll
261-
? _runWatch(
261+
? await _runWatch(
262262
contexts,
263263
configs,
264264
hasDeprecationWarnings,
@@ -267,7 +267,7 @@ const _run = async (
267267
hasteMapInstances,
268268
changedFilesPromise,
269269
)
270-
: _runWithoutWatch(
270+
: await _runWithoutWatch(
271271
globalConfig,
272272
contexts,
273273
outputStream,
@@ -304,11 +304,11 @@ const _runWithoutWatch = async (
304304
onComplete,
305305
changedFilesPromise,
306306
) => {
307-
const startRun = () => {
307+
const startRun = async () => {
308308
if (!globalConfig.listTests) {
309309
preRunMessage.print(outputStream);
310310
}
311-
runJest({
311+
return await runJest({
312312
changedFilesPromise,
313313
contexts,
314314
globalConfig,
@@ -318,7 +318,7 @@ const _runWithoutWatch = async (
318318
testWatcher: new TestWatcher({isWatchMode: false}),
319319
});
320320
};
321-
return startRun();
321+
return await startRun();
322322
};
323323

324324
module.exports = {

packages/jest-cli/src/reporter_dispatcher.js

+17-14
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,27 @@ class ReporterDispatcher {
3636
);
3737
}
3838

39-
onTestResult(test: Test, testResult: TestResult, results: AggregatedResult) {
40-
this._reporters.forEach(
41-
reporter =>
42-
reporter.onTestResult &&
43-
reporter.onTestResult(test, testResult, results),
44-
);
39+
async onTestResult(
40+
test: Test,
41+
testResult: TestResult,
42+
results: AggregatedResult,
43+
) {
44+
for (const reporter of this._reporters) {
45+
reporter.onTestResult &&
46+
(await reporter.onTestResult(test, testResult, results));
47+
}
4548
}
4649

47-
onTestStart(test: Test) {
48-
this._reporters.forEach(
49-
reporter => reporter.onTestStart && reporter.onTestStart(test),
50-
);
50+
async onTestStart(test: Test) {
51+
for (const reporter of this._reporters) {
52+
reporter.onTestStart && (await reporter.onTestStart(test));
53+
}
5154
}
5255

53-
onRunStart(results: AggregatedResult, options: ReporterOnStartOptions) {
54-
this._reporters.forEach(
55-
reporter => reporter.onRunStart && reporter.onRunStart(results, options),
56-
);
56+
async onRunStart(results: AggregatedResult, options: ReporterOnStartOptions) {
57+
for (const reporter of this._reporters) {
58+
reporter.onRunStart && (await reporter.onRunStart(results, options));
59+
}
5760
}
5861

5962
async onRunComplete(contexts: Set<Context>, results: AggregatedResult) {

packages/jest-cli/src/test_runner.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export type TestRunnerOptions = {|
4949
startRun: (globalConfig: GlobalConfig) => *,
5050
|};
5151

52-
type OnTestFailure = (test: Test, err: TestError) => void;
52+
type OnTestFailure = (test: Test, err: TestError) => Promise<*>;
5353
type OnTestSuccess = (test: Test, result: TestResult) => Promise<*>;
5454

5555
const TEST_WORKER_PATH = require.resolve('./test_worker');
@@ -99,24 +99,24 @@ class TestRunner {
9999
timings.length > 0 &&
100100
timings.every(timing => timing < SLOW_TEST_TIME));
101101

102-
const onResult = (test: Test, testResult: TestResult) => {
102+
const onResult = async (test: Test, testResult: TestResult) => {
103103
if (watcher.isInterrupted()) {
104104
return Promise.resolve();
105105
}
106106
if (testResult.testResults.length === 0) {
107107
const message = 'Your test suite must contain at least one test.';
108-
onFailure(test, {
108+
await onFailure(test, {
109109
message,
110110
stack: new Error(message).stack,
111111
});
112112
return Promise.resolve();
113113
}
114114
addResult(aggregatedResults, testResult);
115-
this._dispatcher.onTestResult(test, testResult, aggregatedResults);
115+
await this._dispatcher.onTestResult(test, testResult, aggregatedResults);
116116
return this._bailIfNeeded(contexts, aggregatedResults, watcher);
117117
};
118118

119-
const onFailure = (test: Test, error: TestError) => {
119+
const onFailure = async (test: Test, error: TestError) => {
120120
if (watcher.isInterrupted()) {
121121
return;
122122
}
@@ -128,7 +128,7 @@ class TestRunner {
128128
test.path,
129129
);
130130
addResult(aggregatedResults, testResult);
131-
this._dispatcher.onTestResult(test, testResult, aggregatedResults);
131+
await this._dispatcher.onTestResult(test, testResult, aggregatedResults);
132132
};
133133

134134
const updateSnapshotState = () => {
@@ -149,7 +149,7 @@ class TestRunner {
149149
);
150150
};
151151

152-
this._dispatcher.onRunStart(aggregatedResults, {
152+
await this._dispatcher.onRunStart(aggregatedResults, {
153153
estimatedTime,
154154
showStatus: !runInBand,
155155
});
@@ -194,12 +194,12 @@ class TestRunner {
194194
(promise, test) =>
195195
mutex(() =>
196196
promise
197-
.then(() => {
197+
.then(async () => {
198198
if (watcher.isInterrupted()) {
199199
throw new CancelRun();
200200
}
201201

202-
this._dispatcher.onTestStart(test);
202+
await this._dispatcher.onTestStart(test);
203203
return runTest(
204204
test.path,
205205
this._globalConfig,
@@ -235,11 +235,11 @@ class TestRunner {
235235
// Send test suites to workers continuously instead of all at once to track
236236
// the start time of individual tests.
237237
const runTestInWorker = test =>
238-
mutex(() => {
238+
mutex(async () => {
239239
if (watcher.isInterrupted()) {
240240
return Promise.reject();
241241
}
242-
this._dispatcher.onTestStart(test);
242+
await this._dispatcher.onTestStart(test);
243243
return worker({
244244
config: test.context.config,
245245
globalConfig: this._globalConfig,
@@ -250,8 +250,8 @@ class TestRunner {
250250
});
251251
});
252252

253-
const onError = (err, test) => {
254-
onFailure(test, err);
253+
const onError = async (err, test) => {
254+
await onFailure(test, err);
255255
if (err.type === 'ProcessTerminatedError') {
256256
console.error(
257257
'A worker process has quit unexpectedly! ' +

types/TestRunner.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ export type Reporter = {
2626
test: Test,
2727
testResult: TestResult,
2828
aggregatedResult: AggregatedResult,
29-
) => void,
29+
) => ?Promise<void>,
3030
+onRunStart: (
3131
results: AggregatedResult,
3232
options: ReporterOnStartOptions,
33-
) => void,
34-
+onTestStart: (test: Test) => void,
33+
) => ?Promise<void>,
34+
+onTestStart: (test: Test) => ?Promise<void>,
3535
+onRunComplete: (
3636
contexts: Set<Context>,
3737
results: AggregatedResult,

0 commit comments

Comments
 (0)