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

问题描述:

这是我在ts文件中的函数。angular - ReferenceError:在http:// localhost:9877src/test.ts中找不到变量:URLSearchParams

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

这是我的茉莉花文件。

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

错误:我没有得到因果报应这个错误,但是当我尝试代码覆盖率与yarn test

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

最近有离子2框架同样的问题,我得到这个错误。对我而言,PhantomJS需要为URLSearchParams添加一个polyfill。

因此,尝试包括它作为一个NPM包(或手动https://github.com/WebReflection/url-search-params):

npm install url-search-params 

并将其纳入到任何测试跑步或环境使用的是例如karma配置文件:

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

这是正确的答案,至少一般来说!我通常使用Chrome而不是PhantomJS,所以当我切换到Phantom时,我开始看到这些错误弹出。当我换回时,他们消失了。 –