0

module: https://www.npmjs.com/package/objects-to-csv

I want to use this module in Cypress project.

when I am implementing this module in cypress test it throws below error. enter image description here

I have test using this module in node project and it works as expected.

Example code I am running within Cypress test.

const ObjectsToCsv = require('objects-to-csv');
describe('template spec', () => {
  
  it('objects-to-csv', () => {

    // Sample data - two columns, three rows:
    const data = [
      { code: 'CA', name: 'California' },
      { code: 'TX', name: 'Texas' },
      { code: 'NY', name: 'New York' },
    ];
    
    // If you use "await", code must be inside an asynchronous function:
    (async () => {
      const csv = new ObjectsToCsv(data);
    
      // Save to file:
      await csv.toDisk('./test.csv');
    
      // Return the CSV file as string:
      console.log(await csv.toString());
    })();
  })
})

tsconfig.json:

{ "compilerOptions": { "target": "es2016",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
} }

What I am doing wrong when running this module within Cypress project.

I am trying the simple implementation code for module within Cypress project. https://www.npmjs.com/package/objects-to-csv

Code is throwing an error.

2 Answers 2

0

You are trying to execute a nodejs library in the browser.

The test functions (it() blocks) execute in the browser, not in nodejs, so you don't have access to the underlying filesystem objects (fs).

In order to write to the filesystem in Cypress, you have to create a task and call it from the test.

https://docs.cypress.io/api/commands/task

// cypress.config.js
const { defineConfig } = require('cypress')

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      on('task', {
        async toCSV(data) {
          const csv = new ObjectsToCsv(data);
    
          // Save to file
          await csv.toDisk('./test.csv');
    
          // Return the CSV file as string:
          return await csv.toString();
        },
      })
    },
  },
})


// spec file
it('test', () => {
  const data = [
    { code: 'CA', name: 'California' },
    { code: 'TX', name: 'Texas' },
    { code: 'NY', name: 'New York' },
  ];

  cy.task('toCSV', data).then(csvString => { /* do something */ });
});
Sign up to request clarification or add additional context in comments.

Comments

0

Cypress provides cy.writeFile() to write data from the browser.

Use it instead of csv.toDisk() when running inside the test:

it('objects-to-csv', () => {

  // Sample data - two columns, three rows:
  const data = [
    { code: 'CA', name: 'California' },
    { code: 'TX', name: 'Texas' },
    { code: 'NY', name: 'New York' },
  ];

  // If you use "await", code must be inside an asynchronous function:
  (async () => {
    const csvObj = new ObjectsToCsv(data);
    const csv = await csvObj.toString()

    // Save to file:
    cy.writeFile('./test.csv', csv)

    console.log(csv);
    /*
      code,name
      CA,California
      TX,Texas
      NY,New York
    */

    // Return the CSV file as string:
    return csv
  })();
})

Using a task does not require async/await

The task automatically resolves a promise before returning to the test:

const { defineConfig } = require('cypress')

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      on('task', {
        toCSV(data) {
          const csv = new ObjectsToCsv(data);
          csv.toDisk('./test.csv');   
          return csv.toString();
        },
      })
    },
  },
})
it('objects-to-csv', () => {
  cy.task('toCSV', data)
    .then(cy.log)
})

Test log:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.