reactjs jest jQuery is not defined

流过昼夜 提交于 2020-01-01 03:58:27

问题


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');

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 

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

Thanks


回答1:


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

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

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

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

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



来源:https://stackoverflow.com/questions/41844947/reactjs-jest-jquery-is-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!