gpt4 book ai didi

业力测试中 jQuery 未定义错误

转载 作者:行者123 更新时间:2023-12-01 02:51:43 25 4
gpt4 key购买 nike

我在我的 Angular 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
});
};

有人可以帮忙吗?

提前致谢。

最佳答案

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

您需要将此(请注意 test 配置的 scripts 属性)添加到 angular.json 文件

"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "src/karma.conf.js",
"styles": [],
"scripts": [
"node_modules/jquery/dist/jquery.min.js",
"node_modules/datatables.net/js/jquery.dataTables.js"
]
}
},

关于业力测试中 jQuery 未定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47626692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com