question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error in handleTestEvent fails the entire test suite (no tests marked as being run)

See original GitHub issue

🐛 Bug Report

If an error happens inside a handleTestEvent(event), the entire suite fails to run, even though the error only happens for one of the test cases.

To Reproduce

Place these files in a server/ directory and run as jest server/

jest.config.js

module.exports = {
  projects: [
    {
      rootDir: 'server/',
      testEnvironment: '<rootDir>/CustomEnvironment.js',
      testRunner: 'jest-circus/runner',
    },
  ],
};

CustomEnvironment.js

const NodeEnvironment = require('jest-environment-node');

class CustomEnvironment extends NodeEnvironment {
  handleTestEvent(event) {
    if (event.name === 'test_done' && event.test.errors.length > 0) {
      throw new Error('this will mark the entire suite as failed');
    }
  }
}

module.exports = CustomEnvironment;

foo.test.js

it('pass', () => {
  expect(true).toBeTruthy();
});

it('fail', () => {
  expect(false).toBeTruthy();
});

This gives the following output:

> jest server/
 FAIL  server/foo.test.js
  ● Test suite failed to run

    this will mark the entire suite as failed

       6 |   handleTestEvent(event) {
       7 |     if (event.name === 'test_done' && event.test.errors.length > 0) {
    >  8 |       throw new Error('this will mark the entire suite as failed');
         |             ^
       9 |     }
      10 |   }
      11 | }

      at CustomEnvironment.handleTestEvent (CustomEnvironment.js:8:13)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.042s
Ran all test suites matching /server\//i.

Notice that tests say Tests: 0 total.

Expected behavior

I expect each individual test case that fails in handleTestEvent to be reported as failed in handleTestEvent, but not for all other test cases passing to fail. They should still be reported as having being run successfully.

I expect the tests to say Tests: 1 failed, 1 passed, 2 total, and that maybe the main output for the failure should say ✕ fail (2ms) (failed in handleTestEvent) or similar.

Link to repl or repo (highly encouraged)

envinfo

  System:
    OS: Linux 4.4 Ubuntu 16.04.5 LTS (Xenial Xerus)
    CPU: (4) x64 Intel(R) Xeon(R) Gold 6132 CPU @ 2.60GHz
  Binaries:
    Node: 12.13.0 - ~/.config/nvm/12.13.0/bin/node
    npm: 6.12.0 - ~/.config/nvm/12.13.0/bin/npm
  npmPackages:
    jest: 25.1.0 => 25.1.0

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jeysalcommented, Apr 12, 2020

I think throwing is an internal error of the test runner (not a valid case of an implementation) and should therefore result in ‘Test suite failed to run’. I don’t think we need to distinguish between different events here, most of them would not have a better way of handling than ‘Test suite failed to run’ anyway.

0reactions
thernstigcommented, Apr 11, 2020

Agree that is a solution, and I am using that. I still felt it was weird for the entire suite to be marked as failed? So thought I’d submit this in case you also think this could be handled better, to help some other future user.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I make a JUnit Test Suite that runs tests conditionally ...
Write your on test rule and in this rule ignore (by throwing AssumptionViolatedException) all tests not in your list. Documentation is here:.
Read more >
Configuring Jest
By default, Jest runs all tests and produces all errors into the console upon ... if no tests exist for this file and...
Read more >
junit-team/junit5 - Gitter
if i need to run junit5 tests by tag from maven eg mvn test ... i saw many examples and none of them...
Read more >
Why Your JUnit 5 Tests Are Not Running Under Maven
0 of the Maven Failsafe (integration test runner) plugin. The 2.22.0 releases include support for JUnit. Prior to these releases, to run Junit...
Read more >
jest-circus | Yarn - Package Manager
... of child tests marked with only or todo getting executed even if it is inside a skipped parent describe block (#10806); [jest-jasmine2]...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found