Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle when an error occurs in a beforeEach block of a mocha test. #9

Merged
merged 7 commits into from
Jan 5, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should make them all Set instead of Array, and then simply call tests.add(test) here

});
runner.on('pending', test => pending.push(test));
runner.on('end', () => {
try {
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();
});
16 changes: 13 additions & 3 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,7 +47,7 @@ const toTestResult = ({ stats, tests, jestTestPath, coverage }) => {
sourceMaps: {},
testExecError: null,
testFilePath: jestTestPath,
testResults: tests.map(test => {
testResults: effectiveTests.map(test => {
return {
ancestorTitles: [],
duration: test.duration / 1000,
Expand Down