Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 887
Alex Yong Avatar asked Feb 27 '17 16:02

Alex Yong


1 Answers

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}
    )
});
like image 146
hackerrdave Avatar answered Oct 23 '22 11:10

hackerrdave