7

I need to execute some code after all tests run. I add this test on after hook. But this task needs the report to be created, but on after hook, the report is not created yet.

I also tried to use

 on('run:end', () => {
        console.log("gdfgfdsafkañjsdfjñaldfkjsñkasfdñlassfjdskafmjassd");
  });

but it does nothing.

3 Answers 3

7

You can't, there is an open issue for that.

At the moment you can only leverage a package.json post hook. So, if you have a dedicated command in your package.json file

"scripts": {
  "cy:run": "cypress run"
}

you can add a postcy:run script

"scripts": {
  "cy:run": "cypress run",
  "postcy:run": "<YOUR_COMMAND>"
}

I hope it helps you 😊

1
  • Thanks, I was trying to avoid this solution. I will wait for more answers but if I don't find another one, I will accept yours.
    – Marc
    May 7, 2019 at 6:48
5

You can now listen to after:run events in the plugins file:

on('after:run', (results) => { /* ... */ })

The event fires after the run.

See more info at After Run API

3

Be aware that the post hook might not run when the tests fail. I'm currently using a global after hook in support/index.ts for running after all tests

I'm currently using a global after hook in support/index.ts

after(() => {
  // something here
});
3
  • 1
    Thanks for your response. But the global after is executed before the test report is done, so it is not a good solution for me.
    – Marc
    Jan 14, 2020 at 10:10
  • Can wait be used until the report gets generated?
    – Dhamo
    Jul 30, 2021 at 10:01
  • The reports don't end until the whole process finishes including the after hooks, so I had to the first solution on this post.
    – Marc
    Sep 30, 2021 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.