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

标签 javascript node.js ecmascript-6 jestjs

下面可以在 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 中将 status 更改为 9999,那么所有测试都不会受到影响。我感觉 mockResponseData.json 中的内容根本没有被使用?

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

mockedResponseData.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)
    })
  });

});

getStatusCode.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)
    })
  });

关于javascript - 为什么这个 Jest 测试没有失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64855209/

相关文章:

javascript - 初始 backbone.js 数据获取

javascript - 已定义变量未定义

angularjs - 使用nodejs + angularjs + mongoose加密所有数据

javascript - Nodejs 编码为 Base64 字符串不起作用

javascript - 导入类中的方法未定义

JavaScript:绝对 URL 作为导入中的 es 模块地址

如果当前值大于 10,Javascript 循环添加到前一个值

javascript - 扩展通过 AJAX 返回的 div

javascript - 如何仅使用小写字母验证模式

javascript - 我应该创建一个标量/数字类来防止浮点错误吗?