Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular-cli and Karma with jQuery

I have searched the web and could not find a solution for my current problem, even though seems to be a very common issue...

When running ng test I find that the Karma browser opens and shows nothing:

enter image description here

Also, this is my console:

enter image description here

In order to use jQuery, I have done the things explained here.

This is my lib.ts (basically the same as vendor.ts):

// jQuery
declare let jQuery: any;

// RxJS
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/mergeMap';


// Dependencies
window['jQuery'] = require('jquery');
window['$'] = window['jQuery'];
import 'imports-loader?jQuery=jquery!jquery-color/jquery.color.js';
import 'jquery-ui-npm/jquery-ui.min.js';

require('bootstrap/js/tooltip.js'); // required for X-editable
require('bootstrap/js/popover.js'); // required for X-editable
require('bootstrap/js/dropdown.js'); // required for bootstrap-colorpicker
require('bootstrap/js/tab.js'); //
require('bootstrap/js/modal.js'); //

window['moment'] = require('moment');

import 'imports-loader?jQuery=jquery!jquery-color/jquery.color.js'

I import lib.ts to main.ts.

And finally, the problematic component:

aside-chat-boxes.ts:

'use strict';

declare let $: any;

$.widget("ui.chatbox", {
   // A bunch of stuff here
}

I didn't put all the code of this component for the sake of who's reading.

like image 496
SrAxi Avatar asked Jun 01 '26 07:06

SrAxi


1 Answers

Solved this by adding this to the top part of my component:

import $ from 'jquery/dist/jquery';

It's not the solution that I was looking for, but is a useful workaround.

I'm still amazed of all the problems that we have when using jQuery in an Angular project...

Update (04-01-2018)

The best approach should be to add jQuery to karma.conf.js in the files: [] array. Such as:

files: [
  // imports of dependencies
  ...

  // importing jQuery
  'node_modules/jquery/dist/jquery.min.js'
]
like image 155
SrAxi Avatar answered Jun 03 '26 01:06

SrAxi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!