Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reactjs jest jQuery is not defined

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

like image 574
user3006967 Avatar asked Jan 25 '17 06:01

user3006967


1 Answers

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"] For example:
{
  "jest": {
    "setupFiles": [ "<rootDir>/tests/test-env.js" ]
  }
}
  1. Where the contents on test-env.js look like this
import $ from 'jquery';
global.$ = global.jQuery = $;

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

like image 56
mdsAyubi Avatar answered Nov 17 '22 00:11

mdsAyubi