读取文件时出错:“await”操作数的类型必须是有效的 Promise,或者不得包含可调用的“then”成员

Yun*_*nti 5 javascript async-await

我收到错误:“‘await’操作数的类型必须是有效的 Promise,或者不得包含可调用的‘then’成员。” 当使用基于 Promise .then() 的语法更改测试格式时,该语法适用于使用 async/await。这是我下面的尝试。

 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);
})
Run Code Online (Sandbox Code Playgroud)

前

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

Run Code Online (Sandbox Code Playgroud)

Shu*_*tri 1

readFile方法不返回承诺,因此您不能在其上使用await,但should可以。您可以像这样编写上面的实现

 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);
})
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的回答。奇怪的是,我仍然在“const res = wait csv.should(...)”行上遇到相同的错误。 (2认同)