设置Angular 2单元测试

时间:2016-12-15 17:14:43

标签: angularjs unit-testing phantomjs karma-jasmine

我正在尝试设置Angular 2单元测试。我使用的是Angular版本2.3.0。 Karma和Jasmine似乎正在工作,但测试命令没有正确运行spec文档。

这是我通过webpack和PhantomJS运行的package.json文件:

{
    "name" : "foo",
    "version" : "1.0.0",
    "description" : "Foo",
    "license": "UNLICENSED",
    "scripts" : {
        "start" : "webpack-dev-server --inline --progress --port 8080",
        "build": "rimraf dist && webpack --config config/webpack.prod.js --progress --profile --bail",
        "postinstall" : "typings install",
        "test": "karma start ./node_modules/karma-phantomjs-launcher/karma.conf.js",
        "test:headless": "karma start ./node_modules/karma-phantomjs-launcher/karma.conf.js --browsers PhantomJS"
    },
    "repository" : {
        "type" : "git",
        "url" : "https://xyz/foo.git"
    },
    "dependencies" : {
        "@angular/core" : "2.3.0",
        "@angular/common" : "2.3.0",
        "@angular/compiler" : "2.3.0",
        "@angular/forms" : "2.3.0",
        "@angular/http" : "2.3.0",
        "@angular/platform-browser" : "2.3.0",
        "@angular/platform-browser-dynamic" : "2.3.0",
        "@angular/router" : "3.3.0",
        "rxjs" : "^5.0.0-rc.4",
        "moment" : "^2.17.1",
        "body-parser": "^1.15.1",
        "express": "^4.13.4",
        "systemjs": "^0.19.27",
        "ts-node": "^1.7.2",
        "zone.js" : "^0.7.2"
    },
    "devDependencies" : {
        "angular2-template-loader": "^0.6.0",
        "awesome-typescript-loader": "^3.0.0-beta.14",
        "css-loader": "^0.26.1",
        "extract-text-webpack-plugin": "^1.0.1",
        "file-loader": "^0.9.0",
        "html-loader": "^0.4.4",
        "http-server": "^0.9.0",
        "html-webpack-plugin": "^2.24.1",
        "jasmine-core": "^2.5.2",
        "karma": "^1.3.0",
        "karma-jasmine": "^1.1.0",
        "karma-chrome-launcher": "^2.0.0",
        "karma-phantomjs-launcher": "^1.0.2",
        "karma-sourcemap-loader": "^0.3.7",
        "karma-webpack": "^1.8.0",
        "null-loader": "^0.1.1",
        "phantomjs-prebuilt": "^2.1.14",
        "raw-loader": "^0.5.1",
        "rimraf": "^2.5.4",
        "style-loader": "^0.13.1",
        "to-string-loader": "^1.1.5",
        "typescript": "^2.1.4",
        "typings": "^2.0.0",
        "webpack": "^1.14.0",
        "webpack-dev-server": "^1.16.2",
        "webpack-merge": "^1.1.1"
    }
}

我的规范文件foo.spec.ts旨在失败。

describe('1st test', () => {
    it('true is true', () => expect(true).toBe(false));
});

当我从命令行运行时:

$ npm test foo.spec.ts

我得到了这个结果:

> foo@1.0.0 test /xyz/foo
> karma start ./node_modules/karma-phantomjs-launcher/karma.conf.js "foo.spec.ts"

15 12 2016 09:46:38.156:INFO [karma]: Karma v1.3.0 server started at http://localhost:9876/
15 12 2016 09:46:38.158:INFO [launcher]: Launching browsers PhantomJS, PhantomJS_flags with unlimited concurrency
15 12 2016 09:46:38.165:INFO [launcher]: Starting browser PhantomJS
15 12 2016 09:46:38.177:INFO [launcher]: Starting browser PhantomJS
15 12 2016 09:46:39.236:INFO [PhantomJS 2.1.1 (Mac OS X 0.0.0)]: Connected on socket /#HqReKUy4rzjEewCnAAAA with id 94141501
15 12 2016 09:46:39.241:INFO [PhantomJS 2.1.1 (Mac OS X 0.0.0)]: Connected on socket /#1p069LDXxNe1jTgqAAAB with id 66736253
PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 1 of 1 SUCCESS (0.003 secs / 0.001 secs)
PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 1 of 1 SUCCESS (0.002 secs / 0.001 secs)
TOTAL: 2 SUCCESS

我也试过只运行npm test并获得相同的结果。单元测试似乎运行了两次,但没有识别spec文件。为什么会这样?

修改:我创建了一个包含package.jsonkarma.conf.jsfoo.spec.ts文件的Plunker组。 http://plnkr.co/edit/XsJv0YzpxORCcUXrCwkl

0 个答案:

没有答案