Error reading file: Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member

Viewed 1421

I have the error: 'Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member.' when changing the format of a test from using promise .then() based syntax which worked to use async/await. Which was my attempt below.

 it('downloads successfully', async () => {
        cy.get(downloadButton).click();

        const csv = await cy.readFile(filename, { timeout: 15000 });
        csv.should('have.length.gt', 20);
        expect(csv, 'number of records').to.have.length(10);
})

before

cy.readFile(filename, { timeout: 15000 })
          .should('have.length.gt', 20)
          .then(validateCsv);

1 Answers

readFile method doesn't return a promise and hence you can't use await on it but should does. You can write your above implementation like

 it('downloads successfully', async () => {
        cy.get(downloadButton).click();

        const csv = cy.readFile(filename, { timeout: 15000 });
        const res= await csv.should('have.length.gt', 20);
        expect(res, 'number of records').to.have.length(10);
})
Related