Skip to content
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

Cannot spy on browser Notification constructor function #6129

Closed
bahmutov opened this issue Jan 9, 2020 · 10 comments
Closed

Cannot spy on browser Notification constructor function #6129

bahmutov opened this issue Jan 9, 2020 · 10 comments

Comments

@bahmutov
Copy link
Contributor

bahmutov commented Jan 9, 2020

Cypress v3.8.1
Reproducible example in https://github.com/cypress-io/cypress-test-tiny/tree/spy-on-constructor

This test breaks with the error below

it('spies on Notification', () => {
  cy.spy(window, 'Notification').as('Notification')
  const f = new Notification('Hello')
})

Screen Shot 2020-01-09 at 10 56 44 AM

Note that spying on "regular" constructor functions works

function Foo () {
  if (!new.target) {
    console.log('Foo was called without new!')
    return
  }
  console.log('inside Foo constructor')
}
window.Foo = Foo

it('stubs the constructor', () => {
  cy.stub(window, 'Foo')
  const f = new window.Foo()
  // new automatically converts whatever stub returns into an empty object
  expect(f).to.deep.equal({})
})

and if we wrap Notification with our function that calls new then notifications are working

// workaround - spy on our wrapper of Notification
it('spies on Notification wrapper', () => {
  window.BrowserNotification = (text) => {
    return new Notification(text)
  }
  cy.spy(window, 'BrowserNotification').as('Notification')
  // actual notification will pop up (if Cypress notifications are enabled)
  const f = new BrowserNotification('Hello')
  cy.get('@Notification').should('have.been.calledOnce')
    .and('calledWithNew')
})

Screen Shot 2020-01-09 at 10 56 24 AM

So this seems like sinon.js problem in this case, but still very weird behavior

@bahmutov
Copy link
Contributor Author

bahmutov commented Jan 9, 2020

The place in Sinon.js in our code that calls the function it spies on

Screen Shot 2020-01-09 at 10 48 42 AM

@jennifer-shehane
Copy link
Member

This is fixed in our 4.0 Cypress branch, which includes a Sinon upgrade. #4226

Screen Shot 2020-01-13 at 12 44 31 PM

@bahmutov Is it safe to close this as part of that release?

@bahmutov
Copy link
Contributor Author

can this test be one of e2e tests in 4.0 branch?

it('spies on Notification', () => {
  cy.spy(window, 'Notification').as('Notification')
  const f = new Notification('Hello')
  cy.get('@Notification').should('have.been.calledWith', 'Hello')
})

Then I will feel safe :)

@jennifer-shehane
Copy link
Member

Hmm, I'm a bit unsure of where a test like this should go. @chrisbreiding Do we have any similar tests like this in our repo? Any idea where a test like this would go?

@chrisbreiding
Copy link
Contributor

I think it could go along with the other cy.spy() tests in the driver.

@flotwig
Copy link
Contributor

flotwig commented Feb 3, 2020

Found the original Sinon issue that was fixed in 4.1.3: sinonjs/sinon#1265

@flotwig
Copy link
Contributor

flotwig commented Feb 3, 2020

So... the spying works, but when Cypress goes to serialize the spy for printing, Sinon has a bug that causes it to throw an error. 😄

I opened a bug report here: sinonjs/sinon#2215 Might open a PR, since it looks like a simple fix.

@flotwig
Copy link
Contributor

flotwig commented Feb 3, 2020

Opened a PR over there to address the issue: sinonjs/sinon#2216

Once that gets merged and we update sinon, spying on ALL constructors using cy.spy will be gravy. For now, certain constructors (like Notification) will only work via Cypress.sinon.spy, since that prevents the toString call.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Feb 3, 2020

The code for this is done in cypress-io/cypress#6310, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot cypress-bot bot removed the stage: needs review The PR code is done & tested, needs review label Feb 3, 2020
@flotwig
Copy link
Contributor

flotwig commented Feb 19, 2020

Sinon will release the fix in 9.0.0. Not sure why this issue has been left open... D:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants