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 use cy.readFile or cy.task inside cy.intercept even if promises are supported #15270

Closed
XavierLeTohic opened this issue Mar 1, 2021 · 2 comments
Labels
topic: cy.intercept() type: duplicate This issue or pull request already exists type: unexpected behavior User expected result, but got another

Comments

@XavierLeTohic
Copy link

XavierLeTohic commented Mar 1, 2021

Current behavior

Using both cy.readFile or cy.task inside cy.intercept will occur the following error:

Screenshot 2021-03-01 at 11 22 28

Desired behavior

We generate fixtures files before running Cypress containing multiple possible responses for each query. We would like to read the generated files in order to match the good mock according to the query's variables but we are unable to use cy.readFile or cy.task (that would use fs) inside cy.intercept.

Test code to reproduce

    cy.intercept('POST', 'http://graphql.test/graphql', (req) => {

      req.alias = operationName;

      const operationName = req.body.operationName;
      const [specName] = Cypress.spec.name.split('.');

      cy.readFile(`mocks/generated/${specName}_${req.body.operationName}.json`, (err, data) => {
        console.log(data)
        console.log(err)
      })
      .then((content) => {
        // we want here to find the good response from `content` according to req.body.variables
        req.reply(content);
      });
    });

Versions

Mac OS 11.2.1
Cypress 6.5.0
Node 14.15.3

@XavierLeTohic XavierLeTohic changed the title Cannot use cy.readFile or cy.task inside cy.intercept even if promise are supported Cannot use cy.readFile or cy.task inside cy.intercept even if promises are supported Mar 1, 2021
@jennifer-shehane jennifer-shehane added topic: cy.intercept() type: unexpected behavior User expected result, but got another labels Mar 1, 2021
@jennifer-shehane
Copy link
Member

I can recreate this behavior. I'm not sure what the intended behavior is, but this does seem unexpected.

it('test cy.intercept()', () => {
  cy.intercept('POST', '/users', (req) => {
    cy.readFile('./fixtures/example.json').then((data) => {
      req.reply(data)
    })
  }).as('getUrl')
  cy.visit('https://example.com')
  cy.window().then((win) => {
    const xhr = new win.XMLHttpRequest()
    xhr.open('POST', '/users')
    xhr.send()
  })
  cy.wait('@getUrl')
})

@jennifer-shehane jennifer-shehane added the stage: needs investigating Someone from Cypress needs to look at this label Mar 1, 2021
@flotwig
Copy link
Contributor

flotwig commented Mar 4, 2021

I think this is a duplicate of #7667 - cy commands are incidentally not supported, but it could use a better error either way

@flotwig flotwig closed this as completed Mar 26, 2021
@flotwig flotwig added the type: duplicate This issue or pull request already exists label Mar 26, 2021
@jennifer-shehane jennifer-shehane removed the stage: needs investigating Someone from Cypress needs to look at this label May 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: cy.intercept() type: duplicate This issue or pull request already exists type: unexpected behavior User expected result, but got another
Projects
None yet
Development

No branches or pull requests

3 participants