为什么测试显示 'pass' 虽然期望失败了?
Why does the test show 'pass' although expectation has failed?
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
returns 一个包含 10 个元素的数组。在我的控制台中,我看到测试已通过,这是为什么?
测试表明它通过了,因为它不是在等待承诺解决 - 你需要 return it
函数中的承诺:
it('should have users', () => {
afterEach(() => {
nock.cleanAll()
})
return CrudApi.getAll().then(
data => {expect(data).toHaveLength(9) // this failed
console.log(data.length) // 10}
)
});
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
returns 一个包含 10 个元素的数组。在我的控制台中,我看到测试已通过,这是为什么?
测试表明它通过了,因为它不是在等待承诺解决 - 你需要 return it
函数中的承诺:
it('should have users', () => {
afterEach(() => {
nock.cleanAll()
})
return CrudApi.getAll().then(
data => {expect(data).toHaveLength(9) // this failed
console.log(data.length) // 10}
)
});