未定义reactjs开玩笑的jQuery [英] reactjs jest jQuery is not defined

查看:93
本文介绍了未定义reactjs开玩笑的jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用玩笑来测试我的reactJS组件. 在我的reactJS组件中,我需要使用jquery UI,因此我在组件中添加了它:

I am using jest to test my reactJS component. In my reactJS component, I need to use jquery UI, so I added this in the component:

var jQuery = require('jquery');
require('jquery-ui/ui/core');
require('jquery-ui/ui/draggable');
require('jquery-ui/ui/resizable');

效果很好.但是,现在,我需要使用笑话来进行测试,但是当我将组件加载到testutils中时,我立即遇到了这个问题,

And it worked fine. But, now, I need to used jest to do testing, but I immediately meet this issue when I load the component into testutils,

Test suite failed to run
ReferenceError: jQuery is not defined 

如果您在应用中使用jQuery,有人遇到过这个问题吗?

Has anyone met this issue if you are using jQuery in your app?

谢谢

推荐答案

您可以在全局对象中添加jQuery依赖项.执行以下操作

You can add jQuery dependency in your global object. Do the following

  1. 在package.json中,在玩笑键下添加setupFiles这样的文件 "setupFiles": ["test-env.js"]

  1. In your package.json, under jest key add setupFiles like this "setupFiles": ["test-env.js"]

test-env.js上的内容如下所示

Where the contents on test-env.js look like this

import $ from 'jquery'; global.$ = global.jQuery = $;

import $ from 'jquery'; global.$ = global.jQuery = $;

确保从rootDir到test-env的路径正确. rootDir在Jest中可用.

Make sure the path to test-env is correct from your rootDir. rootDir is available in Jest.

这篇关于未定义reactjs开玩笑的jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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