Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

377
Visualizações
How to interact with a JSON configuration file in Cypress

My frontend depends of a json configuration file that holds various parameters that can modify the content of the page.

When the test starts, my file has a specific content. I would like to find a way to write this file but only for the current test so no state is left for the next test.

This is my current solution, but problem is that if tests fail, the original version of the file is not reset. What could be another approach ?

it('should work a specific way with the given configuration', () => {
    cy.readFile('my-app/parameters.json').then(
      (originalFile) => {
        // Modify the json file however I'd like
        cy.writeFile('my-app/parameters.json', someConfiguration);

        // Test details
      
        cy.writeFile('my-app/parameters.json', originalFile);
      }
    );
  });
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There's nothing really wrong with what you're doing (it's like resetting a database).

But perhaps use a different name for the original?

it('should work a specific way with the given configuration', () => {
    cy.readFile('defaultParameters.json').then(
      (originalFile) => {
        // Modify the json file however I'd like
        cy.writeFile('parameters.json', someConfiguration);

        // Test details
      }
    );
  });

Keep in mind when the test fails mid way you might not write the original back.

Move cy.readFile('defaultParameters.json') to cy.fixture('defaultParameters.json') since Cypress caches the fixture read.

See fixture - Loaded just once

Please keep in mind that fixture files are assumed to be unchanged during the test, and thus the Test Runner loads them just once. Even if you overwrite the fixture file itself, the already loaded fixture data remains the same.

If you wish to dynamically change the contents of a file during your tests, consider cy.readFile() instead.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda