2017-02-27 337 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} 
    ) 
}); 
相關問題