繁体   English   中英

为什么这个 Jest 测试没有失败?

[英]Why doesn't this Jest test fail?

下面可以在https://repl.it/@SandraSchlichti/jest-playground-3#getStatusCode.test.js 上现场试用

最初以下测试的status: 200 ,但我尝试将其更改为301以查看是否了解它的工作原理。

  describe('when the server returns 200', () => {
    let result;
    //??? The desired purpose of this test is to have a mis-match between expected and
    //??? returned status code so getStatusCode() returns 0.
    //??? Trying to figure out how this test works.
    //??? Here I have changed status to 301 and expected the test to fail, but it didn't
    //??? From my understanding mockResponseData contains status 301, and status below
    //??? should override that? But that doesn't seam to be the case for some reason.
    beforeAll(async () => {
      axios.get.mockResolvedValue({
        status: 301,
        data: mockResponseData
      });
      result = await getStatusCode();
    });

    it('should return 0', () => {
      expect(result).toEqual(0)
    })
  });

我原以为测试会失败,当我将其更改为status: 301 ,因为当状态代码匹配时getStatusCode()应该返回1

  1. 为什么测试没有失败?
  2. 是否可以修改测试以使其更清楚预期和返回的状态代码不匹配?
  3. 出于某种原因, statusdata的顺序并不重要。 这是为什么? 不是status: 301假设要覆盖mockResponseData内的status吗?
  4. 如果我里面mockResponseData.json改变status9999 ,然后所有的测试均不受影响。 我觉得mockResponseData.json中的内容mockResponseData.json没有使用?

这些是在本地试用所需的文件:

模拟响应数据.json

  {
    "status": 301,
    "statusText": "Moved Permanently",
    "headers": {
        "location": "https: //www.google.com/",
        "content-type": "text/html; charset=UTF-8",
        "date": "Thu, 12 Nov 2020 10: 09: 41 GMT",
        "expires": "Sat, 12 Dec 2020 10: 09: 41 GMT",
        "cache-control": "public, max-age=2592000",
        "server": "gws",
        "content-length": "220",
        "x-xss-protection": "0",
        "x-frame-options": "SAMEORIGIN",
        "alt-svc": "h3-Q050=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000,h3-T051=\":443\"; ma=2592000,h3-Q046=\":443\"; ma=2592000,h3-Q043=\":443\"; ma=2592000,quic=\":443\"; ma=2592000; v=\"46,43\"",
        "connection": "close"
    }
}

getStatusCode.test.js

const axios = require('axios');
const getStatusCode = require('./getStatusCode');
const mockResponseData = require('./mockResponseData.json');

jest.mock("axios");

describe("getStatusCode ", () => {
  describe('when the server returns status code matching options.statusCode', () => {
    let result;
    beforeAll(async () => {
      axios.get.mockResolvedValue({
        status: 200,
        data: mockResponseData
      })
      result = await getStatusCode({
        statusCode: 200
      })
    });

    it('should return 1', () => {
      expect(result).toEqual(1)
    })
  });

  describe('when the server returns 200', () => {
    let result;
    //??? The desired purpose of this test is to have a mis-match between expected and returned status code,
    //??? getStatusCode() returns 0.
    //??? Trying to figure out how this test works.
    //??? Here I have changed status to 301 and expected the test to fail, but it didn't
    //??? From my understanding mockResponseData contains status 301, and status below should override that?
    //??? But that doesn't seam to be the case for some reason.
    beforeAll(async () => {
      axios.get.mockResolvedValue({
        status: 301,
        data: mockResponseData
      });
      result = await getStatusCode();
    });

    it('should return 0', () => {
      expect(result).toEqual(0)
    })
  });

  describe("when expected and returned status code doesn't match", () => {
    let result;
    beforeAll(async () => {
      // mockRejected value returns rejected promise
      // which will be handled by the try/catch
      axios.get.mockRejectedValue({
        status: 200,
        data: mockResponseData
      })
      result = await getStatusCode();
    })

    it('should return -1', () => {
      expect(result).toEqual(-1)
    })
  });

});

获取状态代码.js

const axios = require('axios');
const qs = require('qs');

module.exports = async (options) => {
  options              = options || {};
  options.url          = options.url || {};
  options.statusCode   = options.statusCode || 0;
  options.timeout      = options.timeout || 1000;
  options.maxRedirects = options.maxRedirects || 0;

  try {
    const response = await axios.get(options.url, {
      timeout: options.timeout,
      maxRedirects: options.maxRedirects,
      // make all http status codes a success
      validateStatus: function (status) {
        return true;
      }
  });


    return (response.status === options.statusCode) ? 1 : 0;
  } catch (error) {
    return -1;
  }
};

当您没有将任何参数传递给result = await getStatusCode(); 它将默认为零。 如果你不想那样做,那就做

describe('when the server returns 200', () => {
    let result;
    beforeAll(async () => {
      axios.get.mockResolvedValue({
        status: 301,
        data: mockResponseData
      });
      result = await getStatusCode({
          statusCode: 200                // <-----------
        });
    });

    it('should return 0', () => {
      expect(result).toEqual(0)
    })
  });

暂无
暂无

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

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