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

编程入门 行业动态 更新时间:2024-10-23 23:23:00
本文介绍了如何在Jest中模拟嵌套函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我收到此错误

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

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

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.

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

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?

在线示例,网址为: 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', 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', 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模块:

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(...);

否则,应将一个函数移至可以模拟的另一个模块,或者应按原样对其进行测试.在这种情况下,可以模拟基础API(https.get).

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.

更多推荐

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

本文发布于:2023-10-31 05:59:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545217.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   函数   如何在   Jest

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!