用Jest在Node中模拟动态需求 [英] Mock dynamic require in Node with Jest

查看:34
本文介绍了用Jest在Node中模拟动态需求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个npm包,该包需要从父/引用包的根目录动态加载依赖项,并且直到运行时才知道该位置,它必须动态地执行以下操作:

Given an npm package that needs to dynamically load a dependency from the root of the parent/referencing package, and that location isn't known until runtime, it has to do a dynamic require:

// config-fetcher.js
const path = require('path');
const getRunningProjectRoot = require('./get-running-project-root');'
module.exports = filename =>
   require(path.resolve(getRunningProjectRoot(), filename));

(无法保证该模块将位于 node_modules 中.它可以被符号链接或全局加载.因此不能使用静态需求.)

(There's no guarantee that the module will be in node_modules. It could be symlinked or loaded globally. So it can't use a static require.)

这是从实际代码中简化的,因此,除非您知道一种相对于正在运行的项目根目录非动态地要求文件的方式,否则必须采用这种方式.

This is simplified from the real code, so unless you know of a way to require files non-dynamically relative to the running project root, this has to be this way.

现在,为了测试这一点,我宁愿不要依赖磁盘上实际存在的任何文件.但是,Jest似乎不允许您模拟不存在的文件.因此,如果我尝试这样做:

Now, to test this, I'd prefer not to depend on any file that's actually on disk. However, Jest seemingly won't let you mock a nonexistent file. So if I try this:

const mockFileContents = {};
jest.mock('/absolute/filename.blah', () => mockFileContents);
// in preparation for wanting to do this:
const result = require('./config-fetcher')('/absolute/filename.blah');
expect(result).toBe(mockFileContents);

然后我从 jest-resolve 中得到一个错误,文件 Resolver.resolveModule 抛出了错误:无法找到模块'/absolute/filename.blah'.

then I get an error from jest-resolve, with file Resolver.resolveModule throwing Error: Cannot find module '/absolute/filename.blah'.

我需要测试此动态需求模块的某些功能,因为它处理相对路径与绝对路径的某些情况,并允许您通过符号指定特殊路径,例如applicationRoot ,因此模块 config-fetcher 可以代替调用者来完成工作.

I need to test some of the functionality of this dynamic requiring module, as it handles some cases of relative paths vs. absolute paths, and allows you to specify a special path through a Symbol, with one being for example applicationRoot, so the module config-fetcher does the hard work instead of the caller.

任何人都可以提供有关如何测试该模块或如何进行重组的指导,从而不需要动态要求,或者它们更易于测试吗?

Could anyone offer guidance on how to test this module, or how to restructure so dynamic requires aren't needed or they are easier to test?

推荐答案

您可以在 详细信息

Jest 完全取代了被测代码的 require 系统.

Jest completely takes over the require system for the code under test.

它具有自己的模块缓存并跟踪模块模拟.

It has its own module cache and keeps track of module mocks.

作为该系统的一部分, Jest 允许您为实际上不存在的模块创建模拟.

As part of this system, Jest allows you to create mocks for modules that do not actually exist.

您可以将 options 作为第三个参数传递给 jest.mock .当前唯一的选择是 virtual ,如果它是 true ,则 Jest 只会将调用模块工厂函数的结果添加到模块缓存中并在要测试的代码中需要时将其返回.

You can pass options as the third parameter to jest.mock. Currently the only option is virtual, and if it is true then Jest will simply add the result of calling the module factory function to the module cache and return it whenever it is required in the code under test.

这篇关于用Jest在Node中模拟动态需求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆