-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Setting stubs/spies in before hooks and automatic restoring #583
Comments
@pawelgalazka could you give an example of a stub/spy that you set multiple stubs - some in |
describe('Google Analytics', () => {
let gaStub
before(() => {
gaStub = cy.stub()
cy.visit('http://localhost:8080', {
onBeforeLoad: (contentWindow) => {
Object.defineProperties(contentWindow, {
'ga': {
value: gaStub,
writable: false
}
})
}
})
})
it('should perform logging on first page load', () => {
cy.wrap(gaStub).should('have.been.called')
})
it('should report pageview', () => {
cy.wrap(gaStub).should('have.been.called.with', 'send', 'pageview')
})
}) This actually works, but following documentation, second test should fail, since stubs should be automatically restored/reset between tests. Is that correct ? BTW. On subject page call |
The stub should be reset between tests regardless of where it's created, so this looks like a bug. I was able to reproduce with this code, where the second test fails but should pass: describe('stub not restored', function () {
let stub
before(function () {
stub = cy.stub()
})
it('should be called', function () {
stub()
expect(stub).to.have.been.called
})
it('should not be called', function () {
expect(stub).not.to.have.been.called
})
}) The driver has undergone (and is still undergoing) a lot of refactoring for |
I'm wondering if this should be treated as a bug, because if I set a stub in If stub is set in |
This brings up a lot of potential discussion points, but I think we are conflating many things which are actually separate and distinct from one another. Point 1.It is true that Cypress will reset stubs, aliases, routes, local storage, cookies, and virtually all state from the browser in between tests. We do not recommend sharing state, and we need to come up with fully fledged lifecycle API's to control this. It is not obvious nor easy to do this, and that's why it doesn't exist yet. Point 2.In your examples above, I'm see the correct behavior. Resetting a stub simply means to replace the existing method that was stubbed with the original. In your example you're pulling out the Point 3.I think our team kind of understands what you're doing. You're wanting to visit once, and then write a bunch of assertions in different tests about the thing you performed once. This conflicts with how we reset state between tests. What you're essentially writing is one giant test. There's not really any benefit splitting things out, because each test is already coupled to the others. |
I’ve just want to address point 3 to explain the benefit of describe('when logged as admin', () => {
before(() => {
// some login commands
cy.visit('http://localhost:8080/some/page')
})
it('should display panel bar', () => {
cy.get('.panel-bar').should('be.visible')
})
it('should should show additional item', () => {
cy.get('.admin-items').should('be.visible')
})
}) There could be a case that additional item could be shown but admin panel bar not at this state (because of some weird error with permissions checks in template), so still there is a benefit of splitting this up to have a better feedback, where we fail exactly. Then there is a question, why not to use Because of this it would be great if it would be possible to disable automatic alias/stubs clearing for certain tests/test files to be able to use them with Hope this explains situation better. |
Somes news right here?
This idea rocks! |
Any news here? |
@moroshko unfortunately I haven't found any workarounds this |
Any update or possible work around for this ? |
Sorry to bother you guys, but I think that it's a very unexpected behavior, do you have some idea when a fix will come? |
+1 . this would be a welcome feature! |
Throwing in a +1 for the "have a way to not reset stubs / spies between tests" option. |
@jennifer-shehane Will you plan to add this feature? |
Will there be any update on this? |
Any update on this? We can't call window.open twice inside a single test. |
Context
From Cypress docs:
How this relates to stubs/spies which were set in
before
hooks ? I would expect that stub/spy which was inbefore
won't be reset/restored for each test within the context. This seems to currently happening actually, but is that intended ?The text was updated successfully, but these errors were encountered: