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

标签 javascript node.js ecmascript-6 jestjs

我收到此错误

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

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

问题

我正在尝试模拟 httpsGet() ,我猜我遇到的问题是因为它不包含在 require() 中,因此 jest .mock('httpsGet') 失败。

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

现场示例: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);
    });
  });
};

最佳答案

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

module.exports.httpsGet = ...

...

module.exports.httpsGet(...);

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

关于javascript - 如何在 Jest 中模拟嵌套函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64860657/

相关文章:

javascript - 在javascript中多次使用扩展运算符?

javascript - 将对象中的文本存储在数组中

angularjs - 在 ubuntu 中使用 npm 安装 karma、jasmine 和 phantomjs 时出现问题

NodeJS 中的 css 解析器和选择器

javascript - 将指令绑定(bind)到 Controller 属性

javascript - 如何为 let block 实现变通方法?

javascript - Visible=false - 没有错误,但 jquery 不起作用

javascript - 是否有更 DRY 的方法来创建 React 文本输入表单元素?

javascript - 在ReactJS中传递参数

javascript - 当所有组件都在一个文件中时,Angular App 可以正常工作,但当组件位于单独的文件中时,会抛出 'nomod' 错误