reactjs jest jQuery 未定义 [英] reactjs jest jQuery is not defined

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

问题描述

我正在使用 jest 来测试我的 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');

而且效果很好.但是,现在,我需要使用jest来做测试,但是当我将组件加载到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?

谢谢

推荐答案

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

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

  1. 在你的 package.json 中,在 jest 键下添加 setupFiles 像这样setupFiles":[test-env.js"] 例如:
  1. In your package.json, under jest key add setupFiles like this "setupFiles": ["test-env.js"] For example:

{
  "jest": {
    "setupFiles": [ "<rootDir>/tests/test-env.js" ]
  }
}

  1. test-env.js 上的内容是这样的
  1. Where the contents on test-env.js look like this

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

确保 test-env.js 的路径在您的根目录中是正确的. 在 Jest 中可用.

Make sure the path to test-env.js is correct from your root directory. <rootDir> is available in Jest.

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

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