简体   繁体   中英

How to mock nested function in Jest?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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