cypress: TypeError: remoteJQuery is not a function

Current behavior:

cy.get(selector) works fine. cy.get(selector).then((elem) => {}) with any callback results in TypeError: remoteJQuery, with no extra information

Desired behavior:

cy.get(selector).then((elem) => {}) should give me access to the element.

Test code:

  it('example', () => {
    cy.get('#cy-div')
    .then(elem => {
      elem;
     })
  })

I’ve tried cy v2.0.4 and v2.1

on Mac OS 10.13.3 using chrome.

screen shot 2018-03-27 at 3 46 33 pm

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 2
  • Comments: 18 (3 by maintainers)

Most upvoted comments

We were in the similar situation, where our website made use of window.$ as something than jQuery.

To give you a surprise, this is what we have:

window.$ = document.querySelector.bind(document)

It really made us worried initially that we would have to change all the occurrences of $(...) to something else in order to make the Cypress work, but luckily, after gloating inside the cypress source code, we found something that could work for us & guess what, it did.

Cypress.on("window:before:load", () => {
  /**
   * Thankfully, Cypress searches for "jQuery" property on the state
   *  variable (cy), if that's present, it takes the precedence
   *  over window.$
   *  https://github.com/cypress-io/cypress/blob/7.0-release/packages/driver/src/cy/jquery.js#L12
   */
  Cypress.cy.state("jQuery", Cypress.$);
});

We hope that this could help others too 😃

@marioparris-qless I think I fixed it with the following code:

cy.on('window:before:load', (win) => {
    if (win.jQuery === undefined && (win.$ === undefined || win.$ === {})) {
        win.$ = Cypress.cy.$$;
    }
});

For anyone else having the same problem, what the code does is, before loading every page in the spec, check if the Window object has the jQuery or $ properties, and if not, assign the jQuery that comes with Cypress to Window.$. As script tags are loaded after this, if the page uses a jQuery library, that one will override the one we set, so the page will still work and we can use jQuery from Cypress.