简体   繁体   English

使用 nockjs 和 jest 进行承诺/异步单元测试的代码覆盖问题

[英]Code coverage concern on promise/asynchronous unit testing using nockjs and jest

I have written a simple unit test for API call using NockJS and Jest for a react application as bellow:我使用 NockJS 和 Jest 为反应应用程序编写了一个简单的 API 调用单元测试,如下所示:

AjaxService.js AjaxService.js

export const AjaxService = {
    post: (url, data, headers) => {
        return axios({
            method: "POST",
            url: url,
            headers: headers || { "content-type": "application/json" },
            data: data
        });
    },
};

API Promise: API承诺:

export const getDashboard = (request) => {
  return AjaxService.post(API_DASHBOARD_URL + "/getDashboard", request
  ).then(
    response => {
      return response.data;
    },
    (error) => {
      return error.response.data;
    }
  )
};

Unit test using NockJS:使用 NockJS 进行单元测试:

nock(baseUrl)
    .post(subUrl, request)
    .reply(200, response);

getDashboard(request)
    .then(resp => {
        let compareVal = getNestedObject(resp, keyToCompare);
        let actualVal = getNestedObject(response, keyToCompare);
        expect(compareVal).to.equal(actualVal);
    })
    .then(() => {});

But when the code-coverage report is generated using Jest --coverage as below:但是当使用 Jest --coverage生成代码覆盖率报告时,如下所示:

在此处输入图片说明

We can see that in promise, success callback and error callback is not called during unit test.我们可以看到,在 promise 中,单元测试期间没有调用成功回调错误回调 How to cover this part of code as it affects code-coverage percentage when an application is having numerous API calls?当应用程序有大量 API 调用时,如何覆盖这部分代码,因为它会影响代码覆盖率? Or am I not testing is properly?还是我没有测试正确? Please help as I am new to unit testing.请帮助,因为我是单元测试的新手。 Thanks in advance.提前致谢。

Jest counts a line as covered only if it runs during a test . Jest在测试期间运行时才将其计为已覆盖。

In this case it looks like you are calling getDashboard during the test...在这种情况下,您getDashboard在测试期间调用了getDashboard ...

...but getDashboard is asynchronous and the test isn't waiting for it to finish. ...但getDashboard是异步的,测试不会等待它完成。

This means that the test completes synchronously before the asynchronous code in getDashboard has a chance to run and any code that hasn't run yet is not included in the Jest code coverage.这意味着测试在getDashboard的异步代码有机会运行之前同步完成,并且任何尚未运行的代码都不包含在Jest代码覆盖率中。

To make sure that code is properly tested and included in the code coverage make sure you await the Promise returned by getDashboard :为了确保代码正确的测试,并包含在代码覆盖率确保您awaitPromise返回由getDashboard

test('getDashboard', async () => {  // <= async test function
  nock(baseUrl)
    .post(subUrl, request)
    .reply(200, response);

  await getDashboard(request)  // <= await the Promise
    .then(resp => {
      let compareVal = getNestedObject(resp, keyToCompare);
      let actualVal = getNestedObject(response, keyToCompare);
      expect(compareVal).to.equal(actualVal);
    })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM