相关疑难解决方法(0)

使用window.require进行Jest测试电子/反应组件

我目前正在创建一个使用React创建界面的Electron应用程序。为了获得fs,我一直在使用:

const fs = window.require('fs');
Run Code Online (Sandbox Code Playgroud)

在电子窗口中工作正常。

问题是,当我为使用window.require('fs')的任何组件编写开玩笑的测试时,在运行测试时出现以下错误。

TypeError: window.require is not a function
Run Code Online (Sandbox Code Playgroud)

我浏览了有关Jest的文档,似乎解决方案是使用手动模拟生成窗口模拟(请参阅https://jestjs.io/docs/zh/manual上的 “ JSDOM中未实现的模拟方法”)-嘲笑)。但是,当我尝试模拟window.require时,请在测试文件的顶部添加

window.require = jest.fn(); 
Run Code Online (Sandbox Code Playgroud)

我仍然得到相同的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();
    });
});
Run Code Online (Sandbox Code Playgroud)

reactjs jestjs electron

5
推荐指数
1
解决办法
432
查看次数

标签 统计

electron ×1

jestjs ×1

reactjs ×1