cypress: Cypress runs test in infinite loop when using cy.writeFile() to a file that is being watched by Cypress

Current behavior:

Cypress GUI looping test forever.

Can not take screenshot because tests are looping fast so I got the video.

loopVideo.zip

My use case:

I have login api function in index.js file and I’m saving response into file.

In tests I use different parts of that json e.g acc token or ref token.

Desired behavior:

Cypress GUI run test once. When user select test and run it. Test should run once.

Steps to reproduce: (app code and test code)

Import

import * as

commands.js

Cypress.Commands.add('apiLoginFe', (body) => {
    querystring.stringify(body)
    return cy.request({
      method: 'POST',
      url: apiUrl+signin,
      form: true,
      body
    })
    .its('body')
})

index.js

before(() => {
  cy.loginRequest(users.admin)
        .then((resp) => {
            cy.writeFile('cypress/data/credential.json', resp.data)
  })
});

Versions

I didn’t try all versions but on 3+ is not working. System OSX, Browsers all

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 16 (6 by maintainers)

Most upvoted comments

Yes, this may be the cause. You’ll need to turn set watchForFileChanges to false as a workaround. More info on file watching here. https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests.html#Configuration

I actually am not able to reproduce it with the above code alone though. I tried this code and it works fine. Am I missing another piece.

it('cy.request() - make an XHR request', () => {
  cy.visit('https://example.cypress.io/commands/network-requests')

  cy.request('https://jsonplaceholder.cypress.io/comments')
    .then((response) => {
      cy.writeFile('cypress/fixtures/example.json', response.body)
    })
})

@jasmeet17 The stackoverflow link was answered by one of our engineers. You cannot have multiple domains within a single test and you cannot have multiple top level describes in a file. If you’re still experiencing this issue - provide an example in a new issue that we can run on our machines.

Unfortunately we have to close the original issue due to inactivity. Please comment if there is new information to provide concerning the original issue and we can reopen.

I moved the file I was writing into another parent folder of the folder with the file with the import, which worked for my case, and that avoided the infinite reload loop! Thanks for delineating this: https://github.com/cypress-io/cypress/issues/3347#issuecomment-713851803

@jennifer-shehane if you add an import, and assuming there’s an initial file in the fixtures folder, it’ll continuously re-run.

import example from '../fixtures/example.json';

it('cy.request() - make an XHR request', () => {
  cy.visit('https://example.cypress.io/commands/network-requests')

  cy.request('https://jsonplaceholder.cypress.io/comments')
    .then((response) => {
      cy.writeFile('cypress/fixtures/example.json', response.body)
    })
})

The looping issue occurred to me when I have run the cy.writeFile several times and overriding the json file. My code below:

cy.request(...).then(response) => {
cy.WriteFile('cypress/fixtures/tokens.json', response.body);
}

I deleted tokens.json and it works fine. The looping only happens when the json file already exists then overwriting the contents of the json file.

@poponuts I had the same problem, and in the end, I realized that this is not a bug. You need to set watchForFileChanges to false because every time you overwrite your JSON file, cypress is listening to changes and restarting the test. I’m wondering now, is there any way to config ignore list related to changes?

The looping issue occurred to me when I have run the cy.writeFile several times and overriding the json file. My code below:

cy.request(...).then(response) => {
  cy.WriteFile('cypress/fixtures/tokens.json', response.body);
}

I deleted tokens.json and it works fine. The looping only happens when the json file already exists then overwriting the contents of the json file.