1

我正在尝试使用单元测试设置 Angular2 项目。我正在使用本手册:http : //twofuckingdevelopers.com/2016/01/testing-angular-2-with-karma-and-jasmine/ 它显然已经过时了,因为现在默认的 Angular2 npm 包是 @angular/*

所述示例不起作用,因为 angular2 包不同。因此,我将所有内容重新映射到该 @angular 包。

我的 package.json 包含以下依赖项:

"dependencies": {
"@angular/common": "^2.0.0-rc.5",
"@angular/compiler": "^2.0.0-rc.5",
"@angular/core": "^2.0.0-rc.5",
"@angular/http": "^2.0.0-rc.5",
"@angular/platform-browser": "^2.0.0-rc.5",
"@angular/platform-browser-dynamic": "^2.0.0-rc.5",
"@angular/router": "^3.0.0-rc.1",
"@angular/router-deprecated": "^2.0.0-rc.2",
"@angular/upgrade": "^2.0.0-rc.5",
"es6-promise": "^3.2.1",
"es6-shim": "^0.35.1",
"reflect-metadata": "^0.1.2",
"rxjs": "5.0.0-beta.6",
"zone.js": "^0.6.17"
},
"devDependencies": {
"@angular/testing": "^0.0.0-0",
"concurrently": "^2.0.0",
"eslint": "^3.3.1",
"eslint-config-google": "^0.6.0",
"eslint-config-standard": "^6.0.0",
"eslint-plugin-promise": "^2.0.1",
"eslint-plugin-standard": "^2.0.0",
"jasmine-core": "2.4.1",
"karma": "^1.2.0",
"karma-chrome-launcher": "^2.0.0",
"karma-coverage": "^1.1.1",
"karma-jasmine": "^1.0.2",
"lite-server": "^2.2.0",
"remap-istanbul": "^0.6.4",
"typescript": "^1.8.10",
"typings": "^1.0.4"
}

我删除了所有 node_packages 文件夹以从零开始。

运行“npm install”时,我看到以下输出:

$ npm install
npm WARN package.json testing-from-twofuckingdevelopers.com@1.0.0 No description
npm WARN package.json testing-from-twofuckingdevelopers.com@1.0.0 No repository field.
npm WARN package.json testing-from-twofuckingdevelopers.com@1.0.0 No README data
npm WARN deprecated tough-cookie@2.2.2: ReDoS vulnerability parsing Set-Cookie https://nodesecurity.io/advisories/130
npm WARN peerDependencies The peer dependency @angular/core@0.0.0-0 included from @angular/testing will no
npm WARN peerDependencies longer be automatically installed to fulfill the peerDependency 
npm WARN peerDependencies in npm 3+. Your application will need to depend on it explicitly.
npm WARN peerDependencies The peer dependency @angular/common@0.0.0-0 included from @angular/testing will no
npm WARN peerDependencies longer be automatically installed to fulfill the peerDependency 
npm WARN peerDependencies in npm 3+. Your application will need to depend on it explicitly.
npm WARN peerDependencies The peer dependency @angular/compiler@0.0.0-0 included from @angular/testing will no
npm WARN peerDependencies longer be automatically installed to fulfill the peerDependency 
npm WARN peerDependencies in npm 3+. Your application will need to depend on it explicitly.
npm WARN peerDependencies The peer dependency @angular/platform-browser@0.0.0-0 included from @angular/testing will no
npm WARN peerDependencies longer be automatically installed to fulfill the peerDependency 
npm WARN peerDependencies in npm 3+. Your application will need to depend on it explicitly.
npm WARN deprecated minimatch@2.0.10: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
|
> fsevents@1.0.14 install /Users/maris/Dropbox/Documents/Knowledge/Development/Angular2/testing-from-twofuckingdevelopers.com/node_modules/karma/node_modules/chokidar/node_modules/fsevents
> node-pre-gyp install --fallback-to-build

[fsevents] Success: "/Users/maris/Dropbox/Documents/Knowledge/Development/Angular2/testing-from-twofuckingdevelopers.com/node_modules/karma/node_modules/chokidar/node_modules/fsevents/lib/binding/Release/node-v46-darwin-x64/fse.node" already installed
Pass --update-binary to reinstall or --build-from-source to recompile

> fsevents@1.0.14 install /Users/maris/Dropbox/Documents/Knowledge/Development/Angular2/testing-from-twofuckingdevelopers.com/node_modules/lite-server/node_modules/browser-sync/node_modules/chokidar/node_modules/fsevents
> node-pre-gyp install --fallback-to-build

[fsevents] Success: "/Users/maris/Dropbox/Documents/Knowledge/Development/Angular2/testing-from-twofuckingdevelopers.com/node_modules/lite-server/node_modules/browser-sync/node_modules/chokidar/node_modules/fsevents/lib/binding/Release/node-v46-darwin-x64/fse.node" already installed
Pass --update-binary to reinstall or --build-from-source to recompile
npm ERR! Darwin 15.6.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install"
npm ERR! node v4.5.0
npm ERR! npm  v2.15.9
npm ERR! code EPEERINVALID

npm ERR! peerinvalid The package @angular/common@2.0.0-rc.5 does not satisfy its siblings' peerDependencies requirements!
npm ERR! peerinvalid Peer @angular/platform-browser@2.0.0-rc.5 wants @angular/common@^2.0.0-rc.5
npm ERR! peerinvalid Peer @angular/platform-browser-dynamic@2.0.0-rc.5 wants @angular/common@^2.0.0-rc.5
npm ERR! peerinvalid Peer @angular/router@3.0.0-rc.1 wants @angular/common@^2.0.0-rc.5
npm ERR! peerinvalid Peer @angular/router-deprecated@2.0.0-rc.2 wants @angular/common@^2.0.0-rc
npm ERR! peerinvalid Peer @angular/testing@0.0.0-0 wants @angular/common@0.0.0-0

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/maris/Dropbox/Documents/Knowledge/Development/Angular2/testing-from-twofuckingdevelopers.com/npm-debug.log

我应该怎么做才能让它工作?或者,也许您可​​以建议涵盖适用于最新 Angular2 版本的单元测试的教程?

4

4 回答 4

1

使用以下命令更新 npm

npm install npm -g
于 2016-11-07T06:36:50.523 回答
0

Angular2 CLI 解决了一个问题: https ://cli.angular.io

它有所有的测试工作。

于 2016-11-07T11:53:15.203 回答
0

我相信你应该遵循 angular.io 网站上的这个指南,它告诉你关于 jasmine-core 以及如何使用 npm 安装它。使用该库,您应该能够进行您尝试设置的测试。

https://angular.io/docs/ts/latest/guide/testing.html

于 2016-08-26T09:40:12.267 回答
0

我发现使用 karma-webpack 运行测试更容易。

请参阅此提交,了解我如何为 angular2 lib 设置测试https://github.com/tb/ng2-api/commit/ef73d95b516579d7aaa9b8b55c906d140abcf3ab

该设置是 angular2-webpack starter https://github.com/preboot/angular2-webpack的缩小版本

于 2016-08-31T20:05:15.843 回答