精华 Use istanbul test coverage on koa
发布于 1年前 作者 fengmk2 903 次浏览 最后一次编辑是 5个月前 来自 分享

Recently, I wrote some koa middlewares: koa-gzip, koa-fresh, and I want to do test coverage with my favorite module: blanket.

But blanket broken #393

$ m cov

/Users/mk2/git/koa-gzip/node_modules/blanket/node_modules/esprima/esprima.js:3872
            throw e;
                  ^
Error: Line 27: Unexpected token *
    at throwError (/Users/mk2/git/koa-gzip/node_modules/blanket/node_modules/esprima/esprima.js:1156:21)
    at throwUnexpected (/Users/mk2/git/koa-gzip/node_modules/blanket/node_modules/esprima/esprima.js:1213:9)

Is esprima not work?

First, I think esprima not support es6. But I search on github found #87 it’s already support es6 now.

istanbul I found

I search ‘coverage’ on npm, istanbul just come out.

There are some articles about makr istanbul work with mocha:

Change blanket to istanbul

All diffs: koa-gzip#9af6

package.json: MUST use istanbul harmony branch

"devDependencies": {
  "autod": "*",
  "contributors": "*",
  "istanbul": "git://github.com/gotwarlost/istanbul.git#harmony",
  "koa": "0.5.0",
  "mm": "0.2.0",
  "mocha": "*",
  "should": "3.1.3",
  "supertest": "0.9.0"
}

Makefile:

install:
 [@npm](/user/npm) install --registry=http://r.cnpmjs.org --cache=${HOME}/.npm/.cache/cnpm

test: install
 [@NODE_ENV](/user/NODE_ENV)=test node --harmony \
  node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha \
  -- -u exports \
  --reporter $(REPORTER) \
  --timeout $(TIMEOUT) \
  $(MOCHA_OPTS) \
  $(TESTS)

stdout:

$ mt


  index.test.js
    gzip()
      ✓ should work with no options
    when status 200 and request accept-encoding include gzip
      ✓ should return gzip string body (39ms)
      ✓ should return raw string body if body smaller than minLength
      ✓ should return raw string body if gzip body bigger than raw body
      ✓ should return gzip buffer body
      ✓ should return gzip stream body
      ✓ should return gzip json body
      ✓ should return number body
    when status 200 and request accept-encoding exclude gzip
      ✓ should return raw body
    when status non 200
      ✓ should return 404
      ✓ should return 500


  11 passing (117ms)

=============================================================================
Writing coverage object [/Users/mk2/git/koa-gzip/coverage/coverage.json]
Writing coverage reports at [/Users/mk2/git/koa-gzip/coverage]
=============================================================================

=============================== Coverage summary ===============================
Statements : 100% ( 26/26 )
Branches : 100% ( 19/19 )
Functions : 100% ( 2/2 )
Lines : 100% ( 26/26 )
================================================================================

istanbul is awesome! I run once tests, got all reports.

There is no more $ make test-cov.

Write for love

Hope this article make your coding life better and more fun.

6 回复

全英文的, 与国际接轨哈.

中文直译。。。

高端大气…

KOA 现在开始用啦?

嗯, cnpmjs.org 已经跑在上门了

回到顶部