Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest doesn't work with global jquery #708

Closed
volodyad opened this issue Feb 12, 2016 · 10 comments
Closed

Jest doesn't work with global jquery #708

volodyad opened this issue Feb 12, 2016 · 10 comments

Comments

@volodyad
Copy link

I have setup jest with webpack according to articles from https://facebook.github.io/jest/docs/getting-started.html. All works fine except I have got third party libraries which requires reference to global jquery. I have configured webpack in the next way
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
})
so it works when I run my app, but when I try to run test it says jQuery is not defined. What can I do with this issue?

@cpojer
Copy link
Member

cpojer commented Feb 15, 2016

You can load jQuery in a setup file, see https://facebook.github.io/jest/docs/api.html#config-setupenvscriptfile-string

window.$ = require('path/to/jquery');

for example.

@cpojer cpojer closed this as completed Feb 15, 2016
@charliecalvert
Copy link

In package.json:

"jest": {
        "setupFiles": ["./source/setup-jest.js"]
 },

In source/setup-jest.js

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

@lux-capacitor
Copy link

Thanks for that @charliecalvert -- Been trying to wire up unit tests to my clients Shopify store repo, this helped a lot!

@cameronmx9
Copy link

In package.json:

"jest": {
        "setupFiles": ["./source/setup-jest.js"]
 },

In source/setup-jest.js

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

Worked for me as well, thank you!

@imskr
Copy link

imskr commented Jul 12, 2019

In package.json:

"jest": {
        "setupFiles": ["./source/setup-jest.js"]
 },

In source/setup-jest.js

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

Worked for me as well, thank you!

Doesn't work for me!

@cardechr
Copy link

cardechr commented Aug 9, 2019

setting up my setup-jest.js file with

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

Does not work for me either. I'm trying to run a test for my bootstrap Modal and says that modal is not a function.

@odan
Copy link

odan commented Sep 25, 2019

This works for me:

global.jQuery = require('jquery');
global.$ = global.jQuery;

@hibachrach
Copy link

If it's not working for you, make sure you're not using ES import and export statements as those are hoisted to the top of the file. Therefore, if you set something on the window/global, it won't apply to subsequent imports. Things will execute in the order you expect if you use require statements.

@maltebaer
Copy link

What worked for me, was adding bootstrap to transformIgnorePatterns in jest.config as suggested in this post.

transformIgnorePatterns: [
    "<rootDir>/node_modules/(?!bootstrap/)",
],

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants