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 1 commit
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
19 changes: 11 additions & 8 deletions src/runMocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ const runMocha = ({ config, testPath, globalConfig }, workerCallback) => {
class Reporter extends Mocha.reporters.Base {
constructor(runner) {
super(runner);
const tests = [];
const pending = [];
const failures = [];
const passes = [];
const tests = new Set();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Mocha 3 works down to node versions that lack Set entirely; if we’re going to use it, let’s be sure to be using a polyfill.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gadzooks, mocha 3 supports all the way back to node 0.10.

My personal preference would be to either:

  • Revert this last commit and keep functionality w/o Set
  • Bump mocha to v4.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The former is much better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I shall await @rogeliog's decision. (This was an async comment, cuz I await'ed in it. I hope my humor transcends the coldness of this comment box. :))

Copy link
Owner

Choose a reason for hiding this comment

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

I agree with @ljharb

const pending = new Set();
const failures = new Set();
const passes = new Set();

runner.on('test end', test => tests.push(test));
runner.on('pass', test => passes.push(test));
runner.on('test end', test => tests.add(test));
runner.on('pass', test => passes.add(test));
runner.on('fail', (test, err) => {
test.err = err;
failures.push(test);
failures.add(test);

// Ensure we include failing tests in our final set of tests.
tests.add(test);
});
runner.on('pending', test => pending.push(test));
runner.on('pending', test => pending.add(test));
runner.on('end', () => {
try {
workerCallback(
Expand Down
17 changes: 4 additions & 13 deletions src/utils/toTestResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,12 @@ const getFailureMessages = tests => {
return failureMessages.length ? failureMessages : null;
};

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);
}
});

const toTestResult = ({ stats, tests: testsSet, jestTestPath, coverage }) => {
const tests = Array.from(testsSet);
return {
coverage,
console: null,
failureMessage: getFailureMessages(effectiveTests),
failureMessage: getFailureMessages(tests),
numFailingTests: stats.failures,
numPassingTests: stats.passes,
numPendingTests: stats.pending,
Expand All @@ -47,7 +38,7 @@ const toTestResult = ({ stats, tests, failures, jestTestPath, coverage }) => {
sourceMaps: {},
testExecError: null,
testFilePath: jestTestPath,
testResults: effectiveTests.map(test => {
testResults: tests.map(test => {
return {
ancestorTitles: [],
duration: test.duration / 1000,
Expand Down