filiphric / cypress-plugin-api

Cypress plugin to show your API information in the Cypress UI runner
ISC License
264 stars 35 forks source link

Ran into TypeError when cy.api() call was made from global hooks #67

Closed kunalashar25 closed 2 years ago

kunalashar25 commented 2 years ago

Hi,

I'm having the following file structure

.
└── project/
    └── cypress/
        ├── e2e/
        │   └── *.cy.ts
        └── support/
            ├── e2e.js
            └── hooks.js

hooks.js contains global hooks. Example:

after('remove automation data', () => {
    removeAutomationData();
});

e2e.js contains

import './hooks';
import 'cypress-plugin-api';

When functions removeAutomationData() internally makes cy.api() request. I'm seeing the following error:

TypeError: Cannot read properties of undefined (reading 'Reproducing global hooks issue.should throw error for global hooks')
Because this error occurred during a `after all` hook we are skipping all of the remaining tests.
    at Context.eval (webpack:///./node_modules/cypress-plugin-api/dist/support.js:3:10683)
Screenshot 2022-11-18 at 7 56 54 AM

Note: If hooks are present directly inside *.cy.ts files then there is no issue in execution.

filiphric commented 2 years ago

Interesting. Can you share what is inside the removeAutomationData() function? The error seems to be pointing to a .should() assertion that trying to access an object attribute, but cannot find it. from the looks of it it seems like some kind of missing reference, but it’s hard to tell honestly

kunalashar25 commented 2 years ago

@filiphric I tried again it seems to be working today, not sure why I ran into an error before. Can mark this as closed for now.