2017-02-27 128 views
1
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个数组。在我的cmd中,我看到测试通过了?jest测试显示通过虽然测试失败

enter image description here

回答

3

试验表明它的消逝,因为它没有等待承诺解决 - 你需要返回的it功能的承诺:

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

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

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