我目前正在创建一个使用 React 创建界面的 Electron 应用程序。为了访问 fs,我一直在使用:
const fs = window.require('fs');
在电子窗口中工作正常。
问题是,当我为使用 window.require('fs') 的任何组件编写开玩笑测试时,我在运行测试时收到以下错误。
TypeError: window.require is not a function
我已经查看了 Jest 的文档,似乎解决方案是使用手动模拟生成窗口模拟(请参阅https://jestjs.io/docs/en/manual 上的“未在 JSDOM 中实现的模拟方法” -模拟)。但是,当我尝试通过在测试文件的顶部添加来模拟 window.require 时
window.require = jest.fn();
我仍然得到相同的 TypeError。
我对创建 Jest 模拟非常陌生,因此非常感谢您对此的任何帮助。
我当前的测试文件(Component.test.js)看起来像
window.require = jest.fn();
import React from 'react';
import renderer from 'react-test-renderer';
import Component from '../index';
describe('Testing', () => {
it('Component renders correctly', () => {
const component = renderer.create(<Component />);
let tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
});