在写入文件然后断言 Cypress.io 测试时,系统抛出 'expected undefined to equal..'

While writing to a file and then asserting a Cypress.io test, the system throws 'expected undefined to equal..'

在写入文件然后断言 Cypress.io 测试时,系统抛出 'expected undefined to equal..'。 'data.json' 文件已成功写入具有名称和电子邮件值的路径。控制台显示 data.json 值。为什么它抛出未定义?

describe('Write to file and verify data', function(){
    it.only('Check whether the writing to file and verify the json data', function(){
        cy.writeFile('../path/to/data.json', { name: 'Apple', email: 'apple@example.com' })
        .then((user) => {
            expect(user.name).to.equal('Apple')
            expect(user.email).to.equal('apple@example.com')            

        })

    })


})

这似乎是文档中的错误或不正确的信息,因为如果您在此处看到 writeFile 的来源:https://github.com/cypress-io/cypress/blob/develop/packages/driver/src/cy/commands/files.coffee已返回,因此您需要对返回的内容执行 JSON.parse :

describe('Write to file and verify data', function(){
    it.only('Check whether the writing to file and verify the json data', function(){
        cy.writeFile('../path/to/data.json', { name: 'Apple', email: 'apple@example.com' })
        .then((user) => {
            let jsonUser = JSON.parse(user)
            expect(jsonUser.name).to.equal('Apple')
            expect(jsonUser.email).to.equal('apple@example.com')            

        })
    })
})