Sandra Schlichting
Sandra Schlichting

Reputation: 25986

Why doesn't this Jest test fail?

Below can be tried out live at https://repl.it/@SandraSchlichti/jest-playground-3#getStatusCode.test.js

Originally the below test had status: 200, but I tried to change it to 301 to see if I had understand how it worked.

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

Question

I had expected the test to fail, when I changed it to status: 301, as getStatusCode() should return 1 when the status codes match.

  1. Why doesn't the test fail?
  2. Can the test be modified to make it more clear the expected and returned status codes doesn't match?
  3. The order of status and data doesn't seam to matter for some reason. Why is that? Isn't status: 301 suppose to override the status inside mockResponseData?
  4. If I inside mockResponseData.json change status to 9999, then all tests are unaffected. I get the feeling that the content in mockResponseData.json isn't used at all?

These are the files needed to try it out locally:

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;
  }
};

Upvotes: 3

Views: 1070

Answers (1)

d2xdt2
d2xdt2

Reputation: 371

When you don't pass any arguments to result = await getStatusCode(); it will default to zero. If you don't want that, then do

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

Upvotes: 2

Related Questions