Cannot find module babel-preset-es2015 in cypress

170 views Asked by At

The erros occurs when i add const replace = require("replace-in-file"); in file to my code

const replace = require("replace-in-file");
            const options = {
                files: "./config/dashboardData.json",
                configFile: true,
                from: /}\n{/g,
                to: ",\n",
            };

I have installed babel and configured the preset still I got the issue

1

There are 1 answers

0
Fody On

The package replace-in-file handles files on the OS, so it needs to be called from Node.

You would need to set up a Cypress task.

In Cypress ver 10+, do this in cypress.config.js

// cypress.config.js

const { defineConfig } = require("cypress");

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      on('task', {
        replaceTask(options) {
          const replace = require("replace-in-file");
          try {
            const results = replace.sync(options);
            return results
          }
          catch (error) {
            return error
          }
        },
      })
    },
    // other e2e configuration here

  },
});
// test.spec.cy.js

it('calls replace-in-file', () => {
  
  const options = {
      files: "./config/dashboardData.json",
      configFile: true,
      from: /}\n{/g,
      to: ",\n",
  };
  cy.task('replaceTask', options).then(resultsOrError => {
    console.log(resultsOrError)
  })
});