Alex Yong
Alex Yong

Reputation: 7645

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}
        )
    });
});

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?

enter image description here

Upvotes: 5

Views: 2518

Answers (1)

hackerrdave
hackerrdave

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

Related Questions