cy.task() calls do not fire in 'test:after:run' event block
See original GitHub issueCurrent 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:
- Created 4 years ago
- Reactions:9
- Comments:19 (5 by maintainers)
Top GitHub Comments
@drewbrend maybe a bit late, but as I can see you can reference the current test in
afterEach
hook with: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