I have a very simple React mixin which uses jQuery to trigger an event
MyMixin = {
trackStructEvent: function () {
args = Array.prototype.slice.call(arguments);
$('body').trigger('myEvent', args);
}
module.exports = MyMixin
This is imported into the main site as part of a new set of components using browserify. As the main site holding these components will always include jQuery, I don't want to require jQuery with browserify, as it will be duplicated.
This isn't an issue in terms of behaviour - however it causes problems when running jest to unit test the components using this mixin throwing the error.
ReferenceError: $ is not defined
I know I can fix this by including jQuery through browserify, but that will load 2 copies into my site.
Is there any way in jest to tell my react component that jQuery already exists on the window and not to worry about it?
var $ = window.jQuery
? – Kuhnsexternal
option in browserify – Kuhnsexternal
seemed to work ok in my component, but I couldn't get it to play nice with jest. For now I have added a check that$
is defined before calling it. I won't put it as an answer as it's not a real solution to the problem. It just let me safely pass my jests – Moweryexternal
option in browserify would be the correct way to go about it. However we're looking to replace jQuery with some smaller modules and polyfils so haven't invested too much time here to get it working – Mowery