How to mock nested function in Jest?

Sandra Schlichting

I am getting this error

Cannot find module 'httpsGet' from 'functions/getSecureString.test.js'

httpsGet() is my own function, and is at the button of getSecureString.js, and called by getSecureString(). httpsGet() uses the https module to get content from a website that requires client side certificates.

Question

I am trying to mock httpsGet() and I am guessing the problem I have is because it isn't included with require() and hence jest.mock('httpsGet') fails.

Can anyone figure out if that is the case, and how I should fix it?

Live example at: https://repl.it/@SandraSchlichti/jest-playground-4

getSecureString.test.js

const getStatusCode = require('./getSectureString');
jest.mock('httpsGet');

describe("getSecureString ", () => {
  describe('when httpsGet returns expected statusCode and body includes expected string', () => {
    let result;
    beforeAll(async () => {
      httpsGet.mockResolvedValue({
        statusCode: 200,
        body: 'xyz secret_string xyz'
      })
      result = await getSecureString({
        hostname:   'encrypted.google.com',
        path:       '/',
        string:     'secret_string',
        statusCode: 200,
        aftaleId:   1234,
        certFile:   1234,
        keyFile:    1234,
        timeout:    1000,
      })
    });

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

  describe('when httpsGet returns expected statusCode and body includes expected string', () => {
    let result;
    beforeAll(async () => {
      httpsGet.mockResolvedValue({
        statusCode: 200,
        body: 'xyz secret_string xyz'
      })
      result = await getSecureString({
        hostname:   'encrypted.google.com',
        path:       '/',
        string:     'not_secret_string',
        statusCode: 201,
        aftaleId:   1234,
        certFile:   1234,
        keyFile:    1234,
        timeout:    1000,
      })
    });

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

  describe("when an exception is thrown", () => {
    let result;
    beforeAll(async () => {
      // mockRejected value returns rejected promise
      // which will be handled by the try/catch
      httpsGet.mockRejectedValue({
        statusCode: 200,
        body: 'xyz secret_string xyz'
      })
      result = await getSecureString();
    })

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

});

getSecureString.js

const fs = require('fs');
const https = require('https');
var uuid = require('uuid');
const {v4: uuidv4} = require('uuid');

module.exports = async function getSecureString(options) {
  options            = options || {};
  options.hostname   = options.hostname || {};
  options.path       = options.path || '/';
  options.string     = options.string || {};
  options.statusCode = options.statusCode || {};
  options.aftaleId   = options.aftaleId || {};
  options.certFile   = options.certFile || {};
  options.keyFile    = options.keyFile || {};
  options.timeout    = options.timeout || 0;

  const opt = {
    hostname: options.hostname,
    port: 443,
    path: options.path,
    method: 'GET',
    cert: fs.readFileSync(options.certFile),
    key: fs.readFileSync(options.keyFile),
    headers: {'AID': options.aftaleId
             },
    };

  opt.agent = new https.Agent(opt);

  try {
    const r = await httpsGet(opt, options.timeout);
    return (r.statusCode === options.statusCode && r.body.includes(options.string)) ? 1 : 0;
  } catch (error) {
    console.error(error);
  }
};

function httpsGet(opts, timeout) {
  return new Promise((resolve, reject) => {
    const req = https.get(opts, (res) => {
      let body = '';
      res.on('data', (data) => {
        body += data.toString();
      });

      res.on('end', () => {
        resolve({body, statusCode: res.statusCode});
      });
    });

    req.setTimeout(timeout, function() {
      req.destroy('error');
    });

    req.on('error', (e) => {
      console.error(e);
      reject(e);
    });
  });
};
Estus Flask

A function that is used in the same module it was declared cannot be spied mocked, unless it's consistently as a method of some object, which is cumbersome and incompatible with ES modules:

module.exports.httpsGet = ...

...

module.exports.httpsGet(...);

Otherwise a function should be moved to another module that can mocked, or should be tested as is. In this case underlying API (https.get) can be mocked instead.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to mock a variable inside a Jest test function?

From Dev

Jest: How should I mock a class function?

From Dev

How to test a return value of a mock function in jest

From Dev

Jest Mock Function?

From Dev

How to force Jest to see nested javascript function?

From Java

how to change jest mock function return value in each test?

From Java

How can I get the arguments called in jest mock function?

From Dev

How can I mock a higher order function in Jest

From Java

How to mock useHistory hook in jest?

From Dev

How to mock a nested method?

From Java

Jest mock callback function from a promise

From Java

Jest mock the same function twice with different arguments

From Dev

Jest: having trouble keeping a reference of a mock function with `jest.fn()`

From Dev

How do I mock a specific function from a module for a specific test (Jest)

From Java

Jest: How to mock one specific method of a class

From Java

How to properly make mock throw an error in Jest?

From Java

How to mock functions in the same module using jest

From Java

How do I set a mock date in Jest?

From Dev

How to mock a global commonjs method with jest?

From Dev

How to mock a decorated function

From Dev

how to mock macro function using google mock

From Dev

how to mock macro function using google mock

From Dev

How to find nested element in jest by value of attribute

From Dev

How to mock constructor of nested class with jmockit

From Dev

How to mock nested properties and objects and their functions?

From Dev

How to mock nested properties and objects and their functions?

From Dev

How to mock jquery ready function

From Dev

Python – How to mock a single function

From Dev

How to stub/mock a const function

Related Related

HotTag

Archive