reactjs - Jest 测试在 react 加载中失败

标签 reactjs jestjs lazy-loading enzyme react-loadable

我们有一个使用 react-loadable 加载的组件,在组件内部我们还有使用 react-loadable 加载的子组件。我 Jest 遇到的问题是无法加载子组件尝试了互联网上的一些建议。请提供帮助,以下是必要的详细信息:

延迟加载

import Loadable from 'react-loadable';
import Loader from './components/ui/genericComponents/Loader';
export const lazyload = (func, fallback) => {
    return Loadable({
        loader: func,
        loading: fallback || Loader,
    });
};

组件.js

import {lazyload} from '../lazyload'
const childComponent1=lazyload(()=>import('./child/childComponent1'));
const childComponent2=lazyload(()=>import('./child/childComponent2'));
const childComponent3=lazyload(()=>import('./child/childComponent3'));

组件.test.js

import React from 'react';
import {configure,mount} from 'enzyme';
import {Component} from '../src/Component';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
configure({ adapter: new Adapter() });

let Wrapper;
beforeAll(() => {
    Wrapper= Enzyme.mount(<Component{...someprops} />);
});

describe('TestSuite for Component',()=>{
//some axios calls
console.log(wrapper.debug()) //I could see the entire wrapper
it('childComponent1',()=>{
console.log(wrapper.debug()) //I only see the loader and not the child Components
})
it('childComponent2',()=>{
console.log(wrapper.debug()) //I only see the loader and not the child Components
})
})

最佳答案

答案是这里第一个与您类似的问题的答案: How test a React Loadable component

这解决了我的问题!

简而言之,可以这样做:

// Import your components
    
import Loadable from 'react-loadable';
    
Loadable.preloadAll()
    
describe('TestName', () => {
    //tests
});

关于reactjs - Jest 测试在 react 加载中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62388532/

相关文章:

reactjs - 如果使用 react with typescript,我可以将 tsx 扩展名用于测试文件吗

javascript - Jest SpyOn 值而不是 getter

hibernate - <h :selectOneMenu> and lazy loading from hibernate

javascript - 在react js应用程序上动态地将参数传递给Web服务

javascript - 在回调中获取上传图像的高度和宽度 -javascript

reactjs - 为什么我的 Jest 测试在使用 Typescript 的 React Native 中失败?

angular - 如何在嵌套延迟加载模块中实现 ngx 翻译(App Module->(Module 1->(Module 2)))

nhibernate - 每个子类继承策略使用 NHibernate 表时的 Casting 代理问题

javascript - setState 正在更新数组中所有元素的 css 属性

javascript - 来自外部文件的流类型导入类型 - 名称已绑定(bind)