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

Cypress generates superfluous screenshots for passing tests too, after afterEach() hook once failed but retried. #9209

Closed
tzolnai opened this issue Nov 16, 2020 · 3 comments · Fixed by #9558

Comments

@tzolnai
Copy link

tzolnai commented Nov 16, 2020

This issue is related to the newer test retries feature. In general, when afterEach() hook fails once all other test cases of the test file are just skipped. However when we have retries enabled (retries : 1), then cypress tries to run the test case again even if it failed in the afterEach() hook. I think somehow the afterEach() hook's failure state stuck, because after the test passes for the second try and so the test goes on with the remaining test cases, cypress creates a screenshot for all remaining tests even if they are not failing. Note that I'm speaking about the screenshot made for the test failures.

Current behavior

Superfluous screenshots are made every time afterEach() hook is called. These screenshots have the 'failed' string in their name, suggesting that a failure happened, however the tests passed.

Desired behavior

Cypress should not generate failure screenshots, when nothing is failing.

Test code to reproduce

describe('Superfluous screenshots.', {retries : 1}, function() {
	afterEach(function() {
		Cypress.env('SOMEVARIABLE', 'X');
		expect(this.currentTest.state).not.to.be.equal('failed');
	});

	it('Failing test which passes for the second time.', function() {
		expect(Cypress.env('SOMEVARIABLE')).to.be.equal('X');
	});

	it('Passing test 1.', function() {
		expect(true).to.be.true;
	});

	it('Passing test 2.', function() {
		expect(false).to.be.false;
	});
});

I used an environment variable to make the first test case and afterEach() hook to fail for the first time and then they pass for the second time, we run the test.

I see the following output when I run this in headless mode:

  Running:  writer/test_spec.js                                                             (1 of 1)


  Superfluous screenshots.
    (Attempt 1 of 2) Failing test which passes for the second time.
    (Attempt 1 of 2) Failing test which passes for the second time.
    ✓ Failing test which passes for the second time. (178ms)
    ✓ Failing test which passes for the second time. (196ms)
    ✓ Passing test 1. (115ms)
    ✓ Passing test 2. (113ms)


  4 passing (4s)


  (Results)

  ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ Tests:        3                                                                                │
  │ Passing:      3                                                                                │
  │ Failing:      0                                                                                │
  │ Pending:      0                                                                                │
  │ Skipped:      0                                                                                │
  │ Screenshots:  6                                                                                │
  │ Video:        false                                                                            │
  │ Duration:     4 seconds                                                                        │
  │ Spec Ran:     writer/test_spec.js                                                              │
  └────────────────────────────────────────────────────────────────────────────────────────────────┘


  (Screenshots)

  -  /home/zolnai/libreoffice/online/cypress_test/cypress/screenshots/writer/test_spe     (1280x720)
     c.js/Superfluous screenshots -- Failing test which passes for the second time (f               
     ailed).png                                                                                     
  -  /home/zolnai/libreoffice/online/cypress_test/cypress/screenshots/writer/test_spe     (1280x720)
     c.js/Superfluous screenshots -- Failing test which passes for the second time --               
      after each hook (failed).png                                                                  
  -  /home/zolnai/libreoffice/online/cypress_test/cypress/screenshots/writer/test_spe     (1280x720)
     c.js/Superfluous screenshots -- Failing test which passes for the second time --               
      after each hook (failed) (attempt 2).png                                                      
  -  /home/zolnai/libreoffice/online/cypress_test/cypress/screenshots/writer/test_spe     (1280x720)
     c.js/Superfluous screenshots -- Failing test which passes for the second time --               
      after each hook (failed) (attempt 2) (1).png                                                  
  -  /home/zolnai/libreoffice/online/cypress_test/cypress/screenshots/writer/test_spe     (1280x720)
     c.js/Superfluous screenshots -- Passing test 1 -- after each hook (failed).png                 
  -  /home/zolnai/libreoffice/online/cypress_test/cypress/screenshots/writer/test_spe     (1280x720)
     c.js/Superfluous screenshots -- Passing test 2 -- after each hook (failed).png                 


====================================================================================================

  (Run Finished)


       Spec                                              Tests  Passing  Failing  Pending  Skipped  
  ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ ✔  writer/test_spec.js                      00:04        3        3        -        -        - │
  └────────────────────────────────────────────────────────────────────────────────────────────────┘
    ✔  All specs passed!                        00:04        3        3        -        -        - 

Note that the second and third test cases pass without any failure, but screenshots were made for them too. I checked the screenshots and they do not show any failure, everything is green.

My expectation here is two screenshots: one for the failure in the test case, the second one is for the failure in afterEach hook. However, we get six screenshots here. Four is about the first test case and two for the other two test cases, which have never failed.

Versions

cypress version: 5.5.0
browser: chromium 78.0.3904.108
OS: openSUSE Leap 15.0

@jennifer-shehane
Copy link
Member

Yeah, this is definitely unintended.

Reproducible example

describe('Superfluous screenshots.', {retries: 1}, function () {
	afterEach(function () {
		Cypress.env('SOMEVARIABLE', 'X')
		expect(this.currentTest.state).not.to.equal('failed')
	})

	it('Failing test which passes for the second time.', () => {
		expect(Cypress.env('SOMEVARIABLE')).to.equal('X')
	})

	it('Passing test 1.', () => {
		expect(true).to.be.true
	})

	it('Passing test 2.', () => {
		expect(false).to.be.false
	})
})

Screen Shot 2020-11-17 at 5 16 37 PM

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Jan 6, 2021

The code for this is done in cypress-io/cypress#9558, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot cypress-bot bot added stage: pending release and removed stage: needs review The PR code is done & tested, needs review labels Jan 6, 2021
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Jan 20, 2021

Released in 6.3.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v6.3.0, please open a new issue.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Jan 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants