简体   繁体   English

angular - ReferenceError:在http:// localhost:9877src / test.ts中找不到变量:URLSearchParams

[英]angular - ReferenceError: Can't find variable: URLSearchParams in http://localhost:9877src/test.ts

This is my function in ts file. 这是我在ts文件中的功能。

getFeatures () {
    const queryString = new URLSearchParams();
    ...
}

This is my jasmine file. 这是我的茉莉花文件。

 it('should get features', inject([FeatureSupportService], (service : FeatureSupportService) => {
    expect(service.getFeatures()).toBeTruthy();
  }));

Error: I am not getting this error in karma but I am getting this error when I try to code coverage with yarn test 错误:我没有在业力中收到此错误,但是当我尝试使用yarn test编码覆盖时,我收到此错误

Can't find variable: URLSearchParams in http://localhost:9877src/test.ts

Had the same problem with Ionic 2 framework recently. 最近与Ionic 2框架有同样的问题。 For me it was that PhantomJS needed a polyfill for URLSearchParams . 对我而言,PhantomJS需要为URLSearchParams提供URLSearchParams

So try including it as a npm package (or manually from https://github.com/WebReflection/url-search-params ): 因此,请尝试将其包含为npm包(或手动从https://github.com/WebReflection/url-search-params ):

npm install url-search-params

And include it to whatever test runner or environment you are using eg karma config file: 并将其包含在您正在使用的任何测试运行器或环境中,例如karma配置文件:

files: [
            '../node_modules/url-search-params/build/url-search-params.js',
            { pattern: './karma-test-shim.js', watched: true }
        ],

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

相关问题 找不到条目模块中的错误:错误:无法解析/src/test.ts - ERROR in Entry module not found: Error: Can't resolve /src/test.ts 如何覆盖 Angular 项目中的 test.ts 文件 - How to cover the test.ts file in an Angular project 迁移到 Angular 15 后,上下文从 test.ts 中删除 - After migration to Angular 15, context was removed from test.ts 从angular4升级到角度5后,npm测试失败“TypeScript编译中缺少test.ts” - npm test fails after upgrade from angular4 to angular 5 “test.ts is missing from the TypeScript compilation” Angular 6单元测试:如何使用test.ts进行业力测试 - Angular 6 Unit testing : how to use test.ts for running karma tests 运行ng测试时如何解决test.ts? - How to resolve test.ts when running ng test? 参考错误:找不到变量:地图 - ReferenceError: Can't find variable: Map ReferenceError:找不到变量:globalThis - ReferenceError: Can't find variable: globalThis 正则表达式:所有以.ts结尾且没有单词index.ts和test.ts的字符串 - Regex: All strings that finishes with .ts and does not have the word index.ts and test.ts 当广告在我的文件上添加代码,就像在视频上一样时,我得到错误“ ./src/app/app.module.ts找不到模块:错误:无法解析'@ angular / http'” - when ad add code on my file as it is on video i get error “./src/app/app.module.ts Module not found: Error: Can't resolve '@angular/http'”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM