Spy On The Application Calling 3rd Party Library Code
Advent day 9: How to check if the application is calling a library method
Imagine we have an application that waits for the 3rd party library and then makes a call. For example, it could be opening a 3rd party chat widget; you can find the sample application and my tests in the repo bahmutov/cypress-3rd-party-script-example
document.getElementById('open-chat')
.addEventListener('click', function () {
if (window.tidioChatApi) {
tidioChatApi.open()
}
})
How do you confirm the “open()” method is called without any arguments by the application in response to the button click? By setting up a spy using the cy.spy command!
Prefer a blog post to the video? Sure, read my “Dealing With 3rd Party Scripts In Cypress Tests“.
Bonus: you can find spy and stub examples in my Spies, Stubs, and Clock collection and in this Cypress Guide.