[英]TypeError: should is not a function
I have recently upgraded to Angular 6 and all the tests that ran fine fail with the following error 我最近升级到Angular 6,所有运行良好的测试均失败,并出现以下错误
TypeError: should is not a function
TypeError:应该不是函数
I am using Angular-Cli 6 and my best guess is this is because appropriate typings aren't available for 'should' (npm-package: https://www.npmjs.com/package/should ) 我正在使用Angular-Cli 6,而我最好的猜测是这是因为'should'(npm-package: https ://www.npmjs.com/package/should)不能使用适当的类型。
I have the following two lines added to every file that uses the should function. 我在使用should函数的每个文件中添加了以下两行。
import should = require('should');
const persist = should; // need to use should library for tests to run https://stackoverflow.com/a/25563303/1373856
Also, I am using "typings": "2.1.1" Anyone faced a similar problem? 另外,我使用的是“类型”:“ 2.1.1”有人遇到过类似的问题吗?
That's not how you include a dependency into your tests (at least, that's not how I learnt it). 那不是您在测试中包含依赖项的方式(至少,这不是我学到的方式)。
Open karma.conf.js
. 打开
karma.conf.js
。 Under the plugins
export, add your dependency : 在
plugins
导出下,添加您的依赖项:
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('should'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
...
Now, in your test files, you just have to declare a variable as a reference to your global function : 现在,在测试文件中,您只需要声明一个变量作为对全局函数的引用:
declare var should: any;
If you have typings, you can also type it to have intellisense in your IDE. 如果您有键入内容,也可以键入它以在您的IDE中具有intellisense。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.