cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.66k stars 3.16k forks source link

The After Run API fails in interactive mode via open even with experimentalInteractiveRunEvents #27335

Open steveguerrero opened 1 year ago

steveguerrero commented 1 year ago

Current behavior

In our project we use cypress in interactive mode and trying to use the after run api to run a delete function after the tests run.

As described in the cypress documentation we use the flag experimentalInteractiveRunEvents in our config.

If you run the interactive mode the after run api doesn't trigger the delete function but displays an error message after closing the Cypress client:

"Error:
Expected DataContext to already have been set via setCtx. If this is a testing context, make sure you are calling "setCtx" in a before hook, otherwise check the application flow."

Desired behavior

The After Run API should trigger when the user closes the browser or probably even better on closing the Cypress client

Test code to reproduce

experimentalInteractiveRunEvents: true,
async setupNodeEvents(on, config) {
        on('after:run', async (results) => {
          await deleteStuff();
        });
    },

Cypress Version

12.17.1

Node version

v18.16.1

Operating System

Windows 10 Enterprise 21H219044.3086

Debug Logs

No response

Other

No response

nagash77 commented 1 year ago

@steveguerrero In order to give our engineers the best chance at recreating your problem, please create a reproducible example using a fork of Cypress Test Tiny. This gives us the best chance of seeing exactly what you are seeing and being able to investigate effectively.

Also check out the Cypress Discord Community Discord chat, it can be helpful for debugging or answering questions on how to use Cypress.

steveguerrero commented 1 year ago

@nagash77 Thanks for your answer. I created a fork from Cypress Test Tiny and I really appreciate your quick reply

https://github.com/steveguerrero/cypress-test-tiny

cypress-app-bot commented 5 months ago

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

steveguerrero commented 5 months ago

Did you find a possible solution or workaround? @jennifer-shehane

thearchitector commented 4 months ago

i also am getting this error, running on Ubuntu 22.04 and Node v20.10.0 with Yarn 4.1.1:

Error: 
      Expected DataContext to already have been set via setCtx. If this is a
      testing context, make sure you are calling "setCtx" in a before hook,
      otherwise check the application flow.

    at c (<embedded>:4315:138737)
    at Object.u (<embedded>:4359:75697)
    at Object.<anonymous> (<embedded>:4392:28989)
    at Object.tryCatcher (~/.cache/Cypress/13.8.1/Cypress/resources/app/node_modules/bluebird/js/release/util.js:16:23)
    at Object.execute (~/.cache/Cypress/13.8.1/Cypress/resources/app/node_modules/bluebird/js/release/method.js:15:34)
    at C.close (<embedded>:4450:26869)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
cene-prometheum commented 2 months ago

Havin the same issue, where the delete function that should be executed after:run won't get executed when closing the runner - same error:

Error: 
      Expected DataContext to already have been set via setCtx. If this is a
      testing context, make sure you are calling "setCtx" in a before hook,
      otherwise check the application flow.

    at c (<embedded>:2746:138737)
    at Object.u (<embedded>:2790:77126)
    at Object.<anonymous> (<embedded>:2823:28750)
    at Object.tryCatcher (~/Library/Caches/Cypress/13.12.0/Cypress.app/Contents/Resources/app/node_modules/bluebird/js/release/util.js:16:23)
    at Object.execute (~/Library/Caches/Cypress/13.12.0/Cypress.app/Contents/Resources/app/node_modules/bluebird/js/release/method.js:15:34)
    at C.close (<embedded>:2881:26880)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
eshadboltadr commented 1 month ago

Me too, same issue when closing Cypress window without running any browser.

on('after:run', async () => {
  console.debug('In after run.');
});
Error: 
      Expected DataContext to already have been set via setCtx. If this is a
      testing context, make sure you are calling "setCtx" in a before hook,
      otherwise check the application flow.

    at c (<embedded>:2746:138779)
    at Object.u (<embedded>:2790:79114)
    at Object.<anonymous> (<embedded>:2823:28750)
From previous event:
    at C.close (<embedded>:2881:26880)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)