-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
spy on win.console.error not working from beforeEach in cypress/support/index.js #4808
Comments
This is because you are trying to spy on Put the beforeEach(() => {
cy.visit('http://localhost:8080/commands/spies-stubs-clocks')
cy.window().then((win) => {
cy.wrap(cy.spy(win.console, 'error')).as('spyWinConsoleError')
cy.wrap(cy.spy(win.console, 'warn')).as('spyWinConsoleWarn')
})
}) Please in the future try asking these types of questions in our community chat first, it can be helpful for debugging or answering questions on how to use Cypress. |
Thank you for the quick response. I did post this question on the community chat before posting it here as an issue but I did not get a response: |
hey ben, maybe this module fits your needs: https://www.npmjs.com/package/cypress-fail-on-console-error |
I am having an issue building a simple test that detects JS errors on a page. I followed your suggestions above but, this simple tests won't work: describe('Smoke - detect JS errors', () => {
beforeEach(() => {
cy.visit('/');
cy.window().then(win => {
cy.wrap(cy.spy(win.console, 'error')).as('spyWinConsoleError');
cy.wrap(cy.spy(win.console, 'warn')).as('spyWinConsoleWarn');
});
});
it('should spy window.confirm', () => {
console.error('questo e errore');
cy.get('@spyWinConsoleError').should('be.calledOnce');
});
}); Can you please advise on this matter? Is it the best way how to create a smoke test for detection of errors in console? |
@borecz I think in your case the console.error is being called before the spy is setup. The example below works because the spy is set up it('should spy window.error', () => {
cy.visit('index.html', {
onBeforeLoad(win) {
cy.spy(win.console, 'error').as('spyWinConsoleError');
cy.spy(win.console, 'warn').as('spyWinConsoleWarn');
},
})
console.error('questo e errore')
cy.get('@spyWinConsoleError').should('be.calledOnce');
}); |
Hi @jennifer-shehane @bahmutov , thank you for your kind reply, unfortunately I was not able to replicate it successfully on my side, please have a look at this tiny repo. here is the repo: |
@jennifer-shehane could you please the repo you used for the screenshot? Updating the url not to a local file also would help. Please check the repo I shared above, it uses your very same script but apparently there are many cases where this won't work. I could not make it work once to be honest. |
Hi all, this perfectly answers the raised points: |
Spying on console methods is described in two recipes here https://github.com/cypress-io/cypress-example-recipes#stubbing-and-spying |
The call to
cy.spy()
onwin.console.error
not working frombeforeEach
incypress/support/index.js
Current behavior:
A spy wrapped onto
win.console.error
does NOT observe a call from my app toconsole.error()
.Desired behavior:
I intended for any call to
console.error("something went wrong")
within my app to cause the cypress test to capture it with the spy.Steps to reproduce: (app code and test code)
My bad, I didn't know about the cypress-test-tiny repo until I started filling out this issue. Here's the steps to reproduce:
git clone --single-branch --branch spying_on_win_console_error https://github.com/benrobot/cypress-example-kitchensink.git
npm install
,npm start
, andnpm run cy:open
)spy_games/the_spy_who_had_nothing.spec.js
Versions
Cypress 3.4.0
Windows 10
Chrome 75
The text was updated successfully, but these errors were encountered: