gpt4 book ai didi

javascript - 为什么测试显示 'pass' 虽然期望失败了?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:21:39 24 4
gpt4 key购买 nike

import React from 'react';
import CrudApi from '../api/CrudApi';
import nock from 'nock';

describe('CrudList Component', () => {

it('should have users', () => {

afterEach(() => {
nock.cleanAll()
})

CrudApi.getAll().then(
data => {expect(data).toHaveLength(9) // this failed
console.log(data.length) // 10}
)
});
});

这是我的测试用例,它应该会失败,因为 getAll 返回一个包含 10 个元素的数组。在我的控制台中,我看到测试已通过,这是为什么?

enter image description here

最佳答案

测试表明它通过了,因为它不是在等待 promise 解决 - 你需要在 it 函数中返回 promise:

it('should have users', () => {

afterEach(() => {
nock.cleanAll()
})

return CrudApi.getAll().then(
data => {expect(data).toHaveLength(9) // this failed
console.log(data.length) // 10}
)
});

关于javascript - 为什么测试显示 'pass' 虽然期望失败了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42491103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com