简体   繁体   English

如何对使用Jquery的Angular 6组件进行单元测试

[英]How to unit test Angular 6 component that uses Jquery

import $ from "jquery";
let newobject = $.extend(true, {}, object);

Cannot read property 'extend' of undefined 无法读取未定义的属性“扩展”

At some point in my Angular 6 application those lines of code execute in my component when I execute ng test. 在我的Angular 6应用程序中的某些时候,当我执行ng test时,这些代码行将在我的组件中执行。 I couldn't find how to make the testing framework know where it can find the library. 我找不到如何使测试框架知道可以在哪里找到库的方法。 I am using Karma and Jasmine. 我正在使用Karma和Jasmine。

I have read the angular.io documentation, tried typings, tried referencing jquery through angular.json file, tried to use the karma-jasmine-jquery library modifying the karma.conf.js file, and also tried @types/jquery. 我已经阅读了angular.io文档,尝试了打字,尝试通过angular.json文件引用了jquery,试图使用karma-jasmine-jquery库修改了karma.conf.js文件,还尝试了@ types / jquery。 I also considered to get rid of that way of cloning an object but since this could be helpful for someone else in some other situation I dared to ask. 我还考虑摆脱克隆对象的方式,但是由于这可能对我敢问的其他情况下的其他人有所帮助。

I am using jquery 3.3.1 installed in node_modules. 我正在使用安装在node_modules中的jquery 3.3.1。

I followed the instructions from the post that cgTag mentioned but it didn't quite help me, what did the trick was to replace 我按照cgTag提到的帖子中的说明进行了操作,但对我没有太大帮助,技巧是替换掉

import $ from "jquery";

for 对于

var $ = require('jquery');

I hope it helps someone else. 我希望它可以帮助其他人。 Don't forget to still Import jquery to karma configuration 不要忘记仍然将jquery导入到业力配置中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM