如何在 Jest 中模拟嵌套函数?

2024-01-16

我收到这个错误

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

httpsGet()是我自己的功能,位于按钮处getSecureString.js,并通过调用getSecureString(). httpsGet()使用https模块从需要客户端证书的网站获取内容。

Question

我试图嘲笑httpsGet()我猜我遇到的问题是因为它不包含在require()因此jest.mock('httpsGet') fails.

谁能弄清楚是否是这种情况,以及我应该如何解决它?

现场示例位于:https://repl.it/@SandraSchlichti/jest-playground-4 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)
    })
  });

});

获取安全字符串.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);
    });
  });
};

在声明的同一模块中使用的函数不能被监视模拟,除非它始终作为某个对象的方法,这很麻烦并且与 ES 模块不兼容:

module.exports.httpsGet = ...

...

module.exports.httpsGet(...);

否则,应该将函数移动到另一个可以模拟的模块,或者应该按原样进行测试。在这种情况下底层 API (https.get) 可以被嘲笑。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Jest 中模拟嵌套函数? 的相关文章