Jonas
Jonas

Reputation: 406

Cypress task with xlsx leads to f.slice when using

I am getting this error when trying to access a cypress task masking the "xlsx": "^0.18.5" for reading excel files.

From Node.js Internals: TypeError: f.slice is not a function at firstbyte (/local0/biologics/gitcheckouts/new-ui-e2e/node_modules/xlsx/xlsx.js:23626:38) at readSync (/local0/biologics/gitcheckouts/new-ui-e2e/node_modules/xlsx/xlsx.js:23706:14)...

My task looks like this cypress.config.ts

      on('task', {
    readXlsx: readXlsx.read,
  });

If I add brackets after the read:-> readXlsx: readXlsx.read() it already fails when starting cypress with the same error message: enter image description here

Does anybody know what to do? (Cypress version 12.5.0)

Add on:

import * as fs from 'fs';

private validateExcelFile(countedItems: number, correction: number) {
cy.get('@LASTDOWNLOADEDFILE').then((name) => {
  const fileName = name as unknown as string;
  cy.log(`Validating excel file: ${fileName}`);
  const buffer = fs.readFileSync(fileName);

  cy.task('readXlsx', { buffer }).then((rows) => {
    const rdw = (rows as string).length;
    expect(rdw).to.be.equal(countedItems - correction);
  });
});

}

Upvotes: 0

Views: 307

Answers (1)

Krieger
Krieger

Reputation: 53

I cannot tell what code, if any, precedes the task. The message seems to indicate a buffer issue, here is my working code you can compare to your own.

Otherwise, suspect the file itself.

import { readFileSync } from "fs";
import { read } from "xlsx/xlsx.mjs";

function read(name) {
  const buffer = readFileSync(name);
  const workbook = read(buffer);
  return workbook
}

...

on('task', {
  readXlsx: (name) => read(name)

...

Upvotes: 3

Related Questions