Search code examples
jqueryangularangular-clikarma-jasmineangular-test

Angular6 and JQuery - layout is not a function with Karma


I'm using a bit of JQuery to work around a visual bug on a library i'm using. It's working well, but now that i'm working toward a tested version, i'm faced with an error.

So, in my component, i have :

declare var $: any;

@Component({ // })
export class MainComponent {
    constructor() {
        $('body').layout('fix')
    }
}

In karma.conf.js, i have the following line :

files: [
  '../node_modules/jquery/dist/jquery.min.js',   
],

And when I launch a ng test, there's this error :

TypeError: $(...).layout is not a function

Any suggestion would be appreciated.


Solution

  • Wrap it around document ready.

    constructor() {
         $(document).ready(function() {
            $('body').layout('fix')
         });
    }   
    

    But I highly recommend using jquery less as possible with angular