虽然测试失败了,但是开始测试显示通过

Ale*_*ong 3 javascript reactjs jestjs enzyme

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}
        )
    });
});
Run Code Online (Sandbox Code Playgroud)

这是我的测试用例,它假设失败,因为getAll返回10个数组.在我的cmd我看到测试通过?

在此输入图像描述

hac*_*ave 9

测试表明它的传递是因为它没有等待解析的承诺 - 你需要在it函数中返回promise :

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

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

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