Create-react-app: Test suite failed: jQuery is not defined

Created on 23 May 2017  路  14Comments  路  Source: facebook/create-react-app

Description

I have imported jquery and the resizable widget in a component in this way:

import $ from "jquery";
import 'jquery-ui/themes/base/resizable.css';
import 'jquery-ui/ui/widgets/resizable';

It worked fine but when I try to run the unit test it returns this error:

Test suite failed to run
ReferenceError: jQuery is not defined

what can I do? Thanks

Most helpful comment

Seems like it works with this:

import $ from "jquery";
import 'jquery-ui/themes/base/resizable.css';

window.jQuery = $;
require('jquery-ui/ui/version');
require('jquery-ui/ui/plugin');
require('jquery-ui/ui/widget');
require('jquery-ui/ui/widgets/mouse');
require('jquery-ui/ui/widgets/resizable');

I sent a PR: https://github.com/FrancescaBarbazeni/jquery-not-defined/pull/1.

I would add that this is pretty broken (on jQuery side) as it鈥檚 not really compatible with modern bundlers, and you might have better luck using React-specific libraries instead. But it seems to work.

Hope it helps!

All 14 comments

@FrancescaBarbazeni Try if this helps

import jQuery from 'jquery';
import 'jquery-ui/themes/base/resizable.css';
import 'jquery-ui/ui/widgets/resizable';

// if you want to use $ as an alias
const $ = jQuery

Thank you @shrynx but nothing changes

Can you provide the full message (with the stack) or a screenshot please?
Which version of jquery do you have in package.json?

yes of course, this is the error:

error

and here the version:

 "jquery": "^3.2.1",
 "jquery-ui": "^1.12.1",

Try this (note the use of require instead of import):

import 'jquery-ui/themes/base/resizable.css'

const $ = require('jquery')
window.jQuery = $;
require('jquery-ui/ui/widgets/resizable')

@timer window will only be available in componentDidMount.
i am able to use jquery with jquery-ui only by aliasing in webpack.
seems jquery-ui has some issue with webpack and all

@Timer with your code the error becomes:

Test suite failed to run  
TypeError: Cannot read property 'mouse' of undefined

Is jQuery UI even compatible with jsdom? I鈥檓 not sure it鈥檚 possible to use it in tests.
I would suggest mocking out the component using jQuery UI so that it鈥榮 not actually used in tests:

// in test file
jest.mock('../MyComponentUsingJQuery', () => <div />)

Of course this would mean you鈥檙e not testing that specific component, but at least it should fix the issues.

@FrancescaBarbazeni

To help more we鈥檇 need to have a GitHub project reproducing the issue. From your previous message, it is hard to say something because the stack is missing.

@gaearon you can find a trivial project with the issue here: https://github.com/FrancescaBarbazeni/jquery-not-defined

I have only modified the App.js file and added jquery and jquery-ui. If you run the test suite you will find the error. I hope this helps you!

Seems like it works with this:

import $ from "jquery";
import 'jquery-ui/themes/base/resizable.css';

window.jQuery = $;
require('jquery-ui/ui/version');
require('jquery-ui/ui/plugin');
require('jquery-ui/ui/widget');
require('jquery-ui/ui/widgets/mouse');
require('jquery-ui/ui/widgets/resizable');

I sent a PR: https://github.com/FrancescaBarbazeni/jquery-not-defined/pull/1.

I would add that this is pretty broken (on jQuery side) as it鈥檚 not really compatible with modern bundlers, and you might have better luck using React-specific libraries instead. But it seems to work.

Hope it helps!

Yes, it definitely works! What do you suggest to use instead? Thank you so much

I鈥檓 not sure really, depends on what exactly you鈥檙e looking for. If I search for "resizable table reactjs" I find a bunch of components, maybe one will match your needs!

i am not able to figure out reference Error:window is not defined while i run the testcase

what can I do? Thanks

@Rajesh-Hegde File a new issue with a reproducing project.

Was this page helpful?
0 / 5 - 0 ratings