Unit testing Jest测试框架中角度服务构造函数的访问
我正在努力找到以下问题的答案(我敢打赌答案很可能是相对简单的) 我正在使用Jest测试框架对一个Ionic 3.4应用程序进行单元测试,不知道如何调用angular服务进行测试,其中构造函数初始化Http对象和InAppBrowser Ionic本机插件对象 我的package.json文件包含以下配置:Unit testing Jest测试框架中角度服务构造函数的访问,unit-testing,typescript,ionic2,ionic3,jestjs,Unit Testing,Typescript,Ionic2,Ionic3,Jestjs,我正在努力找到以下问题的答案(我敢打赌答案很可能是相对简单的) 我正在使用Jest测试框架对一个Ionic 3.4应用程序进行单元测试,不知道如何调用angular服务进行测试,其中构造函数初始化Http对象和InAppBrowser Ionic本机插件对象 我的package.json文件包含以下配置: ... "jest": { "transform": { ".(ts|tsx)": "<rootDir>/node_modules/ts-jest/prep
...
"jest": {
"transform": {
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"json"
]
},
"devDependencies": {
"@types/jest": "^20.0.2",
"@types/node": "^7.0.31",
"jest": "^20.0.4",
"ts-jest": "^20.0.6",
"ts-node": "^3.0.6",
"tslint": "^5.4.3",
"tslint-eslint-rules": "^4.1.1",
"typescript": "2.3.3"
},
...
请求的服务-提供者/settings/settings.ts-如下所示:
{
"compilerOptions" : {
"module" : "commonjs",
"allowSyntheticDefaultImports" : true,
"experimentalDecorators" : true,
"noImplicitAny" : false,
"removeComments" : false,
"locale" : "en-GB",
"alwaysStrict" : true,
"skipLibCheck" : true,
"pretty" : true
}
}
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { InAppBrowser } from '@ionic-native/in-app-browser';
import 'rxjs/add/operator/map';
@Injectable()
export class SettingsProvider {
constructor(public http : Http,
private _INAPP : InAppBrowser)
{ }
// Methods follow here
}
然后在设置.test.ts文件中使用,如下所示:
import 'reflect-metadata';
import { SettingsProvider } from '../providers/settings/settings';
let settings = null;
beforeEach(() => {
// The following works IF the SettingsProvider class
// contains NO parameters in the constructor
settings = new SettingsProvider();
});
describe('Settings service', () =>
{
// Tests are defined within here
// using methods supplied by the
// SettingsProvider class
});
如果我从SettingsProvider类构造函数中取出参数,Jest可以完全按照预期运行测试(通过或失败)
参数位于SettingsProvider类构造函数中时,尝试运行测试脚本时Jest Choke出现以下错误:
Test suite failed to run
/ionic-unit/node_modules/@ionic-native/in-app-browser/index.js:20
import { Injectable } from '@angular/core';
^^^^^^
SyntaxError: Unexpected token import
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:289:17)
at Object.<anonymous> (src/providers/settings/settings:12:21)
at Object.<anonymous> (src/unit-tests/settings.test.ts:2:18)
测试套件无法运行
/爱奥尼亚单位/节点\模块/@爱奥尼亚本地/应用程序内浏览器/索引.js:20
从“@angular/core”导入{Injectable};
^^^^^^
SyntaxError:意外的令牌导入
在ScriptTransformer.\u transformAndBuildScript(节点\u模块/jest运行时/build/ScriptTransformer.js:289:17)
反对。(src/providers/settings/settings:12:21)
反对。(src/unittests/settings.test.ts:2:18)
如何配置测试脚本,以便Jest识别在SettingsProvider构造函数中初始化的模块
我对单元测试还比较陌生,一直在试图弄清楚如何实现这一点(但是,正如您所知,到目前为止还没有多少乐趣!)
我非常感谢任何开发者在这方面的指导/建议
谢谢 我还没有使用玩笑-我通常使用jasmine/karma,这是@angular团队的默认设置-但是你不能使用吗 有了karma/jasmine,你可以做如下事情:
beforeEach(async(() => {
TestBed.configureTestingModule({
providers: [
Http,
InAppBrowser
],
})
}));
beforeEach(async(() => TestUtils.beforeEachCompiler([]).then(compiled => {
fixture = compiled.fixture;
instance = compiled.instance;
fixture.autoDetectChanges(true);
})));
嗨,克里斯蒂安-谢谢你的贡献。我故意用JEST去,因为在离子工程中使用它是非常小的/低的影响(看了其他的配置/安装,以及它们的复杂性,为在离子中使用不同的测试框架做比较)奠定了基础。我还没有在Jest的上下文中研究过使用Testbed,但这是我可以肯定研究的)