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 4.10 breaks nested hooks - Cannot read property 'addCommand' of undefined #8214

Closed
jweingarten opened this issue Aug 6, 2020 · 6 comments · Fixed by #8400
Closed
Assignees
Labels
pkg/driver This is due to an issue in the packages/driver directory topic: hooks ↪ type: regression A bug that didn't appear until a specific Cy version release v4.10.0 🐛 Issue present since 4.10.0

Comments

@jweingarten
Copy link

jweingarten commented Aug 6, 2020

Steps:

  • Create a support/testus.js file with this content
module.exports = function something() {

  beforeEach(() => {
    cy.log('hier');
  });
};
  • Create a support/index.js file with this content
const something = require('./testus.js');

beforeEach(() => {
  cy.log('hier');
  something();
});
  • Create a simple test like
describe('A', function() {
  it.only('B', function () {
    assert.fail('foo');
  });
});
  • Run test with Cypress 4.9 IDE

Expected, as in Cypress 4.9 and previous versions it should log 2 messages.

BEFORE EACH

log hier
log testus
...

image

  • Run test with Cypress 4.10 and above (I also tried the pre release sha 7341476)
BEFORE EACH
log hier
TypeError
Cannot read property 'addCommand' of undefined

Because this error occurred during a before each hook we are skipping all of the remaining tests.

image

Stack trace:

cypress_runner.js:197883 TypeError: Cannot read property 'addCommand' of undefined

Because this error occurred during a `before each` hook we are skipping all of the remaining tests.
    at Test.addCommand (https://somepage.com/__cypress/runner/cypress_runner.js:183869:12)
    at https://somepage.com/__cypress/runner/cypress_runner.js:183368:27
    at RunnablesStore._withTest (https://somepage.com/__cypress/runner/cypress_runner.js:183408:17)
    at RunnablesStore.addLog (https://somepage.com/__cypress/runner/cypress_runner.js:183367:18)
    at EventEmitter.<anonymous> (https://somepage.com/__cypress/runner/cypress_runner.js:181702:22)
    at executeAction (https://somepage.com/__cypress/runner/cypress_runner.js:49450:13962)
    at EventEmitter.n (https://somepage.com/__cypress/runner/cypress_runner.js:49450:13826)
    at EventEmitter.emit (https://somepage.com/__cypress/runner/cypress_runner.js:51593:5)
    at $Cypress.<anonymous> (https://somepage.com/__cypress/runner/cypress_runner.js:197692:19)
    at $Cypress.../driver/node_modules/eventemitter2/lib/eventemitter2.js.EventEmitter.emit (https://somepage.com/__cypress/runner/cypress_runner.js:84157:19)
    at $Cypress.parent.<computed> [as emit] (https://somepage.com/__cypress/runner/cypress_runner.js:170331:33)
    at $Cypress.action (https://somepage.com/__cypress/runner/cypress_runner.js:163930:28)
    at trigger (https://somepage.com/__cypress/runner/cypress_runner.js:171343:22)
    at triggerLog (https://somepage.com/__cypress/runner/cypress_runner.js:171349:12)
    at $Cypress.logFn [as log] (https://somepage.com/__cypress/runner/cypress_runner.js:171418:5)
    at logFn (https://somepage.com/__cypress/runner/cypress_runner.js:163715:26)
From previous event:
    at run (https://somepage.com/__cypress/runner/cypress_runner.js:167240:16)
    at $Cy.cy.<computed> [as log] (https://somepage.com/__cypress/runner/cypress_runner.js:167666:11)
    at __stackReplacementMarker (https://somepage.com/__cypress/runner/cypress_runner.js:166756:13)
    at Context.runnable.fn (https://somepage.com/__cypress/runner/cypress_runner.js:167891:21)
    at callFn (https://somepage.com/__cypress/runner/cypress_runner.js:103248:21)
    at Hook.../driver/node_modules/mocha/lib/runnable.js.Runnable.run (https://somepage.com/__cypress/runner/cypress_runner.js:103235:7)
    at https://somepage.com/__cypress/runner/cypress_runner.js:173106:28
From previous event:
    at Object.onRunnableRun (https://somepage.com/__cypress/runner/cypress_runner.js:173094:20)
    at $Cypress.action (https://somepage.com/__cypress/runner/cypress_runner.js:163887:61)
    at Hook.Runnable.run (https://somepage.com/__cypress/runner/cypress_runner.js:171707:13)
    at next (https://somepage.com/__cypress/runner/cypress_runner.js:103750:10)
    at https://somepage.com/__cypress/runner/cypress_runner.js:103789:7
    at next (https://somepage.com/__cypress/runner/cypress_runner.js:173030:16)
    at https://somepage.com/__cypress/runner/cypress_runner.js:173051:11
From previous event:
    at onNext (https://somepage.com/__cypress/runner/cypress_runner.js:173048:57)
    at done (https://somepage.com/__cypress/runner/cypress_runner.js:103188:5)
    at https://somepage.com/__cypress/runner/cypress_runner.js:103253:11
From previous event:
    at callFn (https://somepage.com/__cypress/runner/cypress_runner.js:103251:14)
    at Hook.../driver/node_modules/mocha/lib/runnable.js.Runnable.run (https://somepage.com/__cypress/runner/cypress_runner.js:103235:7)
    at https://somepage.com/__cypress/runner/cypress_runner.js:173106:28
From previous event:
    at Object.onRunnableRun (https://somepage.com/__cypress/runner/cypress_runner.js:173094:20)
    at $Cypress.action (https://somepage.com/__cypress/runner/cypress_runner.js:163887:61)
    at Hook.Runnable.run (https://somepage.com/__cypress/runner/cypress_runner.js:171707:13)
    at next (https://somepage.com/__cypress/runner/cypress_runner.js:103750:10)
    at https://somepage.com/__cypress/runner/cypress_runner.js:103794:5
    at timeslice (https://somepage.com/__cypress/runner/cypress_runner.js:97720:27)
logError @ cypress_runner.js:197883
(anonymous) @ cypress_runner.js:197530
emit @ cypress_runner.js:51593
(anonymous) @ cypress_runner.js:181785
emit @ cypress_runner.js:51593
emit @ cypress_runner.js:181828
onPrint @ cypress_runner.js:180649
_onPrintClick @ cypress_runner.js:180654
(anonymous) @ cypress_runner.js:182039
executeAction @ cypress_runner.js:49450
n @ cypress_runner.js:49450
ca @ cypress_runner.js:59116
ja @ cypress_runner.js:59117
ka @ cypress_runner.js:59117
wa @ cypress_runner.js:59119
Aa @ cypress_runner.js:59120
ya @ cypress_runner.js:59120
Da @ cypress_runner.js:59123
Ad @ cypress_runner.js:59186
Gi @ cypress_runner.js:59352
Kb @ cypress_runner.js:59141
Dd @ cypress_runner.js:59188
(anonymous) @ cypress_runner.js:59353
../../node_modules/scheduler/cjs/scheduler.production.min.js.exports.unstable_runWithPriority @ cypress_runner.js:63493
Ii @ cypress_runner.js:59353
Cd @ cypress_runner.js:59187
With Cypress 4.10 these 2 things are listed as features, and I think are the reason for the breakage. ```You can open a before, beforeEach, after, and afterEach hook definition in your IDE from the Test Runner's Command Log by clicking the Open in IDE button. Addresses #7793.before, beforeEach, after, and afterEach hook definitions now display separately in the Test Runner's Command Log when defined in separate hook definitions. Addresses #7779.```I do realize that it is an anti-pattern to have hooks like this defined instead of in the tests, but as long as I don't care in what order they are called i should be able to use them. Why do I have code like this? Well actually I went trough some of the plugins you have listed in your documentation and they do it. I borrowed the code to implement similar features specific to our product, which now stopped working and I am stuck with Cypress 4.9.0.This ticket is similar to https://github.com/cypress-io/cypress/issues/8086. The fix for https://github.com/cypress-io/cypress/issues/8086 is in https://github.com/cypress-io/cypress/pull/8113. As mentioned above I tried pre-release https://github.com/cypress-io/cypress/commit/7341476f0f3c2c548969d1e9cbff08038c41e7a9, which should include the fix from PR https://github.com/cypress-io/cypress/pull/8113, but it doesn't fix my use case above. This is what I used to test with the pre-release and as said, it still fails. ```export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/5.0.0/darwin-x64/circle-develop-7341476f0f3c2c548969d1e9cbff08038c41e7a9-414411/cypress.zipnpm install https://cdn.cypress.io/beta/npm/5.0.0/circle-develop-7341476f0f3c2c548969d1e9cbff08038c41e7a9-414404/cypress.tgz`
@jennifer-shehane
Copy link
Member

What you are essentially doing with this require statement is the below, defining a beforeEach within a defined beforeEach. I'm not sure if this code should have ever worked - although the error message is not very clear about why this isn't working.

Your something function should just export the cy.log() call, so that it does not define another beforeEach.

I'm going to double check with the team on this one though.

This error is likely being thrown from this line of code: https://github.com/cypress-io/cypress/blob/develop/packages/reporter/src/runnables/runnables-store.ts#L183:L183 which was changed in #7821

Repro

beforeEach(() => {
  cy.log('foo')
  
  beforeEach(() => {
    cy.log('bar')
  })
})

it('test', () => {})

4.9.0

Screen Shot 2020-08-07 at 2 53 18 PM

4.10.0

Screen Shot 2020-08-07 at 2 53 32 PM

@jennifer-shehane jennifer-shehane added type: regression A bug that didn't appear until a specific Cy version release v4.10.0 🐛 Issue present since 4.10.0 labels Aug 7, 2020
@cypress-bot cypress-bot bot added the stage: ready for work The issue is reproducible and in scope label Aug 7, 2020
@jennifer-shehane jennifer-shehane added pkg/driver This is due to an issue in the packages/driver directory topic: hooks ↪ internal-priority labels Aug 7, 2020
@jennifer-shehane
Copy link
Member

I tried this out in basic Mocha and this nesting of beforeEach does work, so I think this should be a valid example that we should support.

let num = 0
console.log(num) // logs 0

beforeEach(() => {
  num += 1
  console.log(num) // logs 1

  beforeEach(() => {
    num += 1
    console.log(num) // logs 2
  })
})

it('test', () => { 
  num += 1
  console.log(num) // logs 3
})

@jennifer-shehane jennifer-shehane changed the title Cypress 4.10 breaks hooks (aftereach/beforeeach) - Cannot read property 'addCommand' of undefined Cypress 4.10 breaks nested hooks - Cannot read property 'addCommand' of undefined Aug 7, 2020
@jennifer-shehane
Copy link
Member

I guess there's some disagreement with the team on whether this should be supported. Either way, I think this error should be handled in some way - or it should error if you nest hooks. So, we'll leave this open to evaluate more.

@jweingarten
Copy link
Author

Thx @jennifer-shehane. I reworked all of our code. This is no longer blocking me. I do think its a very fragile piece and may need more documentation and better error handling.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Sep 1, 2020

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

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Sep 1, 2020

Released in 5.1.0.

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

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Sep 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
pkg/driver This is due to an issue in the packages/driver directory topic: hooks ↪ type: regression A bug that didn't appear until a specific Cy version release v4.10.0 🐛 Issue present since 4.10.0
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants