使用 Jest 在 Node 中模拟动态 require

2023-11-23

给定一个 npm 包需要从父/引用包的根动态加载依赖项,并且该位置直到运行时才知道,它必须执行动态 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。它可以被符号链接或全局加载。所以它不能使用静态要求。)

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

现在,为了测试这一点,我不想依赖磁盘上实际存在的任何文件。然而,Jest 似乎不会让你模拟一个不存在的文件。所以如果我尝试这个:

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投掷Error: Cannot find module '/absolute/filename.blah'.

我需要测试这个动态需求模块的一些功能,因为它处理相对路径与绝对路径的某些情况,并允许您通过符号指定特殊路径,例如applicationRoot,所以模块config-fetcher代替调用者做艰苦的工作。

任何人都可以提供有关如何测试此模块或如何重组以便不需要动态需求或者更容易测试的指导吗?


你可以通过{ virtual: true } as options in jest.mock模拟一个不存在的模块:

const { myFunc } = require('does not exist');

jest.mock('does not exist',
  () => ({
    myFunc: () => 'hello'
  }),
  { virtual: true }
);

test('mock file that does not exist', () => {
  expect(myFunc()).toBe('hello');  // Success!
});

Details

Jest完全接管require被测试代码的系统。

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

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

你可以通过options作为第三个参数jest.mock。目前唯一的选择是virtual,如果是true then Jest将简单地将调用模块工厂函数的结果添加到模块缓存中,并在测试代码中需要时返回它。

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

使用 Jest 在 Node 中模拟动态 require 的相关文章

随机推荐