Skip to content

Commit

Permalink
Handle when an error occurs in a beforeEach block of a mocha test. (#9)
Browse files Browse the repository at this point in the history
* Handle when an error occurs in a beforeEach block of a mocha test.

* fix node 4

* trigger another build

* PR Feedback: Conver to use Sets

* Revert "PR Feedback: Conver to use Sets"

This reverts commit f8a5439.

* Create correct type for failureMessagse property.

* re-trigger build
  • Loading branch information
hswolff authored and rogeliog committed Jan 5, 2018
1 parent 690a95f commit b293dd8
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* eslint-disable no-unreachable */
const assert = require('assert');

describe('My tests', () => {
it('This test passes', () => {
assert.equal(1, 1);
});

describe('Nested describe', () => {
beforeEach(() => {
throw new Error('Error in nested beforeEach');
});

it('This nested test passes', () => {
assert.equal(1, 1);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
runner: '../../../',
};
17 changes: 17 additions & 0 deletions integrationTests/__snapshots__/errorInBeforeEach.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Works when it has an error inside of beforeEach 1`] = `
"FAIL integrationTests/__fixtures__/errorInBeforeEach/__mocha__/__tests__/file.test.js
My tests Nested describe \\"before each\\" hook for \\"This nested test passes\\"
Error: Error in nested beforeEach
at mocked-stack-trace
✓ This test passes
\\"before each\\" hook for \\"This nested test passes\\"
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 passed, 2 total
Snapshots: 0 total
Time:
Ran all test suites.
"
`;
5 changes: 5 additions & 0 deletions integrationTests/errorInBeforeEach.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const runJest = require('./runJest');

it('Works when it has an error inside of beforeEach', () => {
return expect(runJest('errorInBeforeEach')).resolves.toMatchSnapshot();
});
5 changes: 4 additions & 1 deletion src/runMocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ const runMocha = ({ config, testPath, globalConfig }, workerCallback) => {

runner.on('test end', test => tests.push(test));
runner.on('pass', test => passes.push(test));
runner.on('fail', test => failures.push(test));
runner.on('fail', (test, err) => {
test.err = err;
failures.push(test);
});
runner.on('pending', test => pending.push(test));
runner.on('end', () => {
try {
Expand Down
16 changes: 10 additions & 6 deletions src/utils/__tests__/__snapshots__/toTestResult.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ Error stack
Object {
"ancestorTitles": Array [],
"duration": 0.004,
"failureMessages": "
"failureMessages": Array [
"
This test failed
The error message
Expand All @@ -53,6 +54,7 @@ Error stack
Error stack
",
],
"fullName": "This test failed",
"numPassingAsserts": 1,
"status": "failed",
Expand Down Expand Up @@ -90,7 +92,7 @@ Object {
Object {
"ancestorTitles": Array [],
"duration": 0.001,
"failureMessages": null,
"failureMessages": Array [],
"fullName": "This test passes[1]",
"numPassingAsserts": 0,
"status": "passed",
Expand Down Expand Up @@ -130,7 +132,7 @@ Object {
Object {
"ancestorTitles": Array [],
"duration": 0.001,
"failureMessages": null,
"failureMessages": Array [],
"fullName": "This test passes[1]",
"numPassingAsserts": 0,
"status": "passed",
Expand Down Expand Up @@ -181,7 +183,7 @@ Error stack
Object {
"ancestorTitles": Array [],
"duration": 0.001,
"failureMessages": null,
"failureMessages": Array [],
"fullName": "This test passes[1]",
"numPassingAsserts": 0,
"status": "passed",
Expand All @@ -190,7 +192,7 @@ Error stack
Object {
"ancestorTitles": Array [],
"duration": 0.004,
"failureMessages": null,
"failureMessages": Array [],
"fullName": "This test also passes[2]",
"numPassingAsserts": 0,
"status": "passed",
Expand All @@ -199,7 +201,8 @@ Error stack
Object {
"ancestorTitles": Array [],
"duration": 0.004,
"failureMessages": "
"failureMessages": Array [
"
This test failed
The error message
Expand All @@ -211,6 +214,7 @@ Error stack
Error stack
",
],
"fullName": "This test failed",
"numPassingAsserts": 1,
"status": "failed",
Expand Down
4 changes: 4 additions & 0 deletions src/utils/__tests__/toTestResult.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ it('turns a passing mocha tests to Jest test result', () => {
failures: 0,
},
tests: [passingTest],
failures: [],
}),
).toMatchSnapshot();
});
Expand All @@ -58,6 +59,7 @@ it('turns a passing mocha tests to Jest test result with coverage', () => {
failures: 0,
},
tests: [passingTest],
failures: [],
}),
).toMatchSnapshot();
});
Expand All @@ -74,6 +76,7 @@ it('turns a failing mocha tests to Jest test result', () => {
failures: 1,
},
tests: [failingTest],
failures: [],
}),
).toMatchSnapshot();
});
Expand All @@ -90,6 +93,7 @@ it('turns a whole mocha tests suite to Jest test result', () => {
failures: 0,
},
tests: [passingTest, passingTest2, failingTest],
failures: [],
}),
).toMatchSnapshot();
});
19 changes: 15 additions & 4 deletions src/utils/toTestResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@ const getFailureMessages = tests => {
return failureMessages.length ? failureMessages : null;
};

const toTestResult = ({ stats, tests, jestTestPath, coverage }) => {
const toTestResult = ({ stats, tests, failures, jestTestPath, coverage }) => {
const effectiveTests = tests;

// Merge failed tests that don't exist in the tests array so that we report
// all tests even if an error occurs in a beforeEach block.
failures.forEach(test => {
if (!tests.some(t => t === test)) {
tests.push(test);
}
});

return {
coverage,
console: null,
failureMessage: getFailureMessages(tests),
failureMessage: getFailureMessages(effectiveTests),
numFailingTests: stats.failures,
numPassingTests: stats.passes,
numPendingTests: stats.pending,
Expand All @@ -37,11 +47,12 @@ const toTestResult = ({ stats, tests, jestTestPath, coverage }) => {
sourceMaps: {},
testExecError: null,
testFilePath: jestTestPath,
testResults: tests.map(test => {
testResults: effectiveTests.map(test => {
const failureMessage = toMochaError(test);
return {
ancestorTitles: [],
duration: test.duration / 1000,
failureMessages: toMochaError(test),
failureMessages: failureMessage ? [failureMessage] : [],
fullName: test.fullTitle(),
numPassingAsserts: hasError(test) ? 1 : 0,
status: hasError(test) ? 'failed' : 'passed',
Expand Down

0 comments on commit b293dd8

Please sign in to comment.