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.

Cypress will not stop test run if errors in beforeEach/afterEach

See original GitHub issue
  • Operating System: MacOS and Docker image (cypress/base:8 and node:8.5)
  • Cypress Version: 0.20.1
  • Browser Version: Electron

Is this a Feature or Bug?

Bug

Current behavior:

If an error is raised in both the beforeEach and afterEach hooks for tests, Cypress will not stop the test run and seems to continue recording video indefinitely (video file keeps increasing).

Desired behavior:

Tests should fail with a descriptive error message.

How to reproduce:

Run the test below.

node_modules/.bin/cypress run will “hang” and continue running until killed node_modules/.bin/cypress run --headed will also “hang”, but you can see the errors in the log and see the timer continuing to run.

Test code:

describe("cypress", function() {
  beforeEach(function() {
    // cy.visit("/");
    throw new Error();
  });

  afterEach(function() {
    // Commenting this out will allow the test to finish and stop recording
    // cy.visit("/");
    throw new Error();
  });

  it("doesn't hang when both beforeEach and afterEach fail", function() {
  });
});

Additional Info (images, stack traces, etc)

  • The original test makes a cy.visit call and the tests would hang if the server didn’t respond with a 200.
  • Canceling the --headed run results in output similar to the following:
  cypress
    1) "before each" hook for "doesn't hang when both beforeEach and afterEach fail"

  0 passing (15s)
  1 failing

  1) cypress "before each" hook for "doesn't hang when both beforeEach and afterEach fail":
     E

Because this error occurred during a 'before each' hook we are skipping the remaining tests in the current suite: 'cypress'


  Because this error occurred during a 'before each' hook we are skipping the remaining tests in the current suite: 'cypress'r

  Because this error occurred during a 'before each' hook we are skipping the remaining tests in the current suite: 'cypress'o

  Because this error occurred during a 'before each' hook we are skipping the remaining tests in the current suite: 'cypress'r

  Because this error occurred during a 'before each' hook we are skipping the remaining tests in the current suite: 'cypress'

Looking at the “random” character after ‘cypress’, it starts to spell out: Error at Context. <anonymous> (http://localhost:64422/__cypress/tests

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
brian-manncommented, Feb 18, 2018

The fundamental problem about this isn’t about errors - it’s about state. When a test has its state updated and no properties actually change (due to a perfect storm of problems) - the reporter will never update the UI, and Cypress will never take a screenshot and continue on. That’s the area it’s failing at.

1reaction
brian-manncommented, Feb 18, 2018

I am not sure how this slipped through the cracks but it should have been fixed awhile ago. I’ll put it on a milestone so that we actually fix it…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cypress basics: before(), beforeEach(), after() and afterEach()
This article explains how these hooks work and how to use them effectively. ... Let's say you have a spec that has a...
Read more >
Test Retries - Cypress Documentation
If the test fails a third time, Cypress will mark the test as failed and then move on to run any remaining tests....
Read more >
Retry only it- and afterEach-block if something fails in ...
You can find details Cypress Test Retries. When each test is run again, the following hooks will be re-run also: beforeEach afterEach.
Read more >
11 - Working with Cypress Hooks | beforeEach | afterEach
Here you will learn, what are the cypress hooks and how is useful?Additional InfoLink for SDET/QA Interview Full Play List ...
Read more >
The Most Common Cypress Mistakes - Christian Lüdemann
To fix the problem above, we must either combine commands (so we are retrying the whole todo item selector) or alternate commands and...
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