Javascript 在写入文件并断言Cypress.io测试时,系统抛出';预期未定义为等于..';

Javascript 在写入文件并断言Cypress.io测试时,系统抛出';预期未定义为等于..';,javascript,cypress,Javascript,Cypress,在写入文件并断言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

在写入文件并断言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的来源:很明显,如果内容是对象,则内容会被字符串化,并且内容会被返回,因此您需要对返回的内容执行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')            

        })
    })
})