在业力测试中,jQuery没有定义错误

时间:2017-12-04 04:49:09

标签: jquery angular5 systemjs

我在我的角度5应用程序中使用jQuery,它运行得很好,但是我遇到了业力测试的问题。

import { ElementRef } from '@angular/core';
declare var jQuery: any;

export class Item {

constructor(private element: ElementRef) {
    this.init();
}

private init() {
    this.$item = jQuery(this.element.nativeElement);
...

错误

ReferenceError:未定义jQuery。

的package.json

"dependencies": {
  "jquery": "3.1.1"
}

我已将jquery添加到我的karma.conf中,但这没有帮助。

files: [
        'node_modules/core-js/client/shim.js',
        'node_modules/systemjs/dist/system.src.js',
        'node_modules/zone.js/dist/zone.js',
        'node_modules/zone.js/dist/proxy.js',
        'node_modules/zone.js/dist/sync-test.js',
        'node_modules/zone.js/dist/jasmine-patch.js',
        'node_modules/zone.js/dist/async-test.js',
        'node_modules/zone.js/dist/fake-async-test.js',
        'node_modules/tslib/tslib.js',
        'node_modules/jquery/dist/jquery.js',

        {pattern: 'node_modules/rxjs/**/*.js', included: false, watched: false },
        {pattern: 'node_modules/rxjs/**/*.js.map', included: false, watched: false },
        {pattern: '../karma-test-shim.js', included: true, watched: true},
        {pattern: 'node_modules/@angular/**/*.js', included: false, watched: false},
        {pattern: 'node_modules/@angular/**/*.js.map', included: false, watched: false},
        {pattern: 'node_modules/@ng-idle/core/bundles/core.umd.js', included: false, watched: false},
        {pattern: 'node_modules/bowser/bowser.js', included: false, watched: false},
        {pattern: 'node_modules/bootstrap/dist/js/bootstrap.js', included: false, watched: false},
        {pattern: 'node_modules/tslib/tslib.js', included: false, watched: false},
        {pattern: 'node_modules/jquery/dist/jquery.js', included: false, watched: false}
    ],
    proxies: {
        "/node_modules/": "../../node_modules/"
    },
    autoWatch: true,
    browsers: ['PhantomJS', 'Chrome', 'HeadlessChrome'],
    singleRun: false
});
};
你能帮忙吗?

提前致谢。

3 个答案:

答案 0 :(得分:2)

jQuery

中加入karma.conf.js路径
module.exports = function(config) {
  config.set({
    files: [
      'path_to/jquery.js'
      ...
    ],
  });
};

答案 1 :(得分:1)

从Angular 6 / Karma 1.7.1 / Jasmine 2.99 +开始,以上答案似乎已经过时。因为他们要求从karma.conf.js中删除文件和预处理器条目。

您需要将此文件(请注意SELECT (d._rowid = n.debtorid) AS `Enforcement Allocated` FROM note n LEFT JOIN debtor d ON n.debtorid = d._rowid LEFT JOIN fee f ON f.debtorid = d._rowid WHERE f.type = 'Enforcement' AND n.type = 'Stage'; 配置的CASE属性)添加到scripts文件中

test

答案 2 :(得分:0)

看起来你已经两次添加了jquery:

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

        .....
        {pattern: 'node_modules/jquery/dist/jquery.js', included: false, watched: false}
    ],

您第二次添加它时,您已明确声明您不想包含它。我想你只需要删除第二行。