Reputation: 7645
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}
)
});
});
This is my test case, it's supposed to fail because getAll
returns an array with 10 elements. In my console I'm seeing that the test passed, why is that?
Upvotes: 5
Views: 2518
Reputation: 6706
The test indicates its passing because it is not waiting for the promise to resolve - you need to return the promise in the it
function:
it('should have users', () => {
afterEach(() => {
nock.cleanAll()
})
return CrudApi.getAll().then(
data => {expect(data).toHaveLength(9) // this failed
console.log(data.length) // 10}
)
});
Upvotes: 14