question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cy.task() calls do not fire in 'test:after:run' event block

See original GitHub issue

Current behavior:

Attempting to fire cy.task() commands after a test completes appears to outright fail.

Desired behavior:

At the end of each test, I would expect the cy.task command to fire, running a block of code within the task registered in cypress/plugins/index.js

Steps to reproduce: (app code and test code)

Simple Test

it('Can navigate to Account page', () => {
    cy.get('nav').within(() => {
      cy.getByText('Account').should('have.attr', 'href', '/parent/settings');
    });
  });

In cypress/support/index.js I have the following event listener enabled:

Cypress.on('test:after:run', (testAttr, runnable) => {
    cy.task('logTestResults', {
      title: `${runnable.parent.title}${runnable.title}`,
      state: runnable.state,
      err: runnable.err,
    });
  });

And that should fire this task in cypress/plugins/index.js:

on('task', {
      logTestResults({ title, state, err }) {
        console.log('made it to the task');
        const tags = [];
        tags.push(`outcome:${state}`);
        tags.push(`description:${changeCase.dot(title)}`)
        if (err) {
          tags.push(`error:${changeCase.dot(err.name)}`);
          tags.push(`error.message:${changeCase.dot(err.message)}`);
        }
        dogapi.metric.send(`e2e`, 1, {tags:tags}, function(err, results){
          console.dir(results);
        });

        return null
      }
    });

Through console logging I can tell that the test:after:run event listener is working, as console logs appear in the cypress runner’s JS console. However console logs within the plugin task do not get written to my terminal.

However, if I add a cy.task command DIRECTLY to my test like so:

it('Can navigate to Account page', () => {
    cy.task('logTestResults', {title: 'test', state: 'failed', err:{name:'testerr', message:'test err message'}});
    cy.get('nav').within(() => {
      cy.getByText('Account').should('have.attr', 'href', '/parent/settings');
    });
  });

The task registered in the plugins file DOES catch the command and logs the faked details I pass it. Why is that same cy.task command not working in my test:after:run event listener?

Versions

Cypress: 3.4.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:9
  • Comments:19 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
Hazzard17hcommented, Jan 26, 2021

@drewbrend maybe a bit late, but as I can see you can reference the current test in afterEach hook with:

afterEach(() => {
  // @ts-ignore
  const test = cy.state('runnable')?.ctx?.currentTest;
  if (test) {
    // stuffs here
  }
});
3reactions
bahmutovcommented, Dec 13, 2019

We got to add cy.state method to our typescript definitions even if just a few properties, then our docs would be enough for people to build whatever they want I think

Sent from my iPhone

On Dec 12, 2019, at 19:11, Brian Mann notifications@github.com wrote:

The test:after:run event is synchronous and will not respect async cypress commands - and likely never will. Hooks before, beforeEach, afterEach, and after are the only places you’ll be able to put cypress commands because of how Cypress must tie hooks + tests together.

I understand what you all are trying to do - getting access to the currentTest properties from inside of a hook. This data should already be available to you as cy.state(‘runnable’).currentTest.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

Read more comments on GitHub >

github_iconTop Results From Across the Web

task | Cypress Documentation
The task plugin event handler can return a value or a promise. The command will fail if undefined is returned or if the...
Read more >
Jul 25 2019 12:35 UTC - cypress-io/cypress - Gitter
What is the suggested way to call into npm scripts? ... above? cy.task() commands don't fire within test:after:run event listener block.
Read more >
Posting result data to a website (like API or a telegram bot ...
What I've seen from Cypress help page is that cy.task() calls do not fire in 'test:after:run' event block github.com/cypress-io/cypress/issues/ ...
Read more >
cypress: Versions - Openbase
Fixed an issue where cy.type('{enter}') was not sending the Enter key for Firefox ... Fixed an issue with .type() where click events could...
Read more >
The ultimate fake id guide v9.rar download
Fixes #15032. cy.log() will now show all arguments, not only the ... Addresses #8889. .type() now fires the beforeInput event during typing.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found