单个istanbul命令用于多个脚本(或组合覆盖报告)

问题描述 投票:1回答:2

我想在他们自己的过程中运行几个测试,并以某种方式结合伊斯坦布尔报告。

例如,两个实现:

//sut1.js
'use strict'
module.exports = function() {
  return 42
}

//sut2.js
'use strict'
module.exports = function() {
  return '42'
}

和两个测试:

//test1.js
'use strict'
const expect = require('chai').expect
const sut1 = require('./sut1.js')
expect(sut1()).to.equal(42)
expect(sut1()).not.to.equal('42')
console.log('looks good')

和:

//test2.js
'use strict'
const expect = require('chai').expect
const sut2 = require('./sut2.js')

describe('our other function', function() {
  it('should give you a string', function() {
    expect(sut2()).to.equal('42')
  })

  it('should not give a a number', function () {
    expect(sut2()).not.to.equal(42)
  })
})

我可以得到这样一个报道的报道:

istanbul cover --print both test1.js
istanbul cover --print both -- node_modules/mocha/bin/_mocha test2.js

获得合并报道报告的最简单方法是什么?是否还有一个内衬也会输出它?

使用mocha或jasmine,你可以传入多个文件,但在这里我想实际运行不同的脚本。

javascript testing code-coverage istanbul
2个回答
2
投票

如果有人有兴趣,回答是:

  • 不要使用伊斯坦布尔;使用nyc - 这样你就可以将可执行文件传递给它而不仅仅是javascript文件
  • 将两个测试放在一个bash文件中,然后用istanbul运行

...

#! /usr/bin/env bash
# test.sh

set -e

node test1.js
node_modules/mocha/bin/mocha test2.js

然后像这样去

nyc ./test.sh

你会看到组合测试输出:

----------|----------|----------|----------|----------|----------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
All files |      100 |      100 |      100 |      100 |                |
 sut1.js  |      100 |      100 |      100 |      100 |                |
 sut2.js  |      100 |      100 |      100 |      100 |                |
 test1.js |      100 |      100 |      100 |      100 |                |
 test2.js |      100 |      100 |      100 |      100 |                |
----------|----------|----------|----------|----------|----------------|

你也可以在package.json脚本中这样做:

"_test": "node test1.js && mocha test2.js",
"test": "nyc npm run _test",

1
投票

自从我上次回答以来,如果不能将所有测试合并到一个调用中,我已经发现如何实际组合报告。

#! /usr/bin/env bash
# test.sh

set -e

COMBINED_OUTPUT=nyc_output

rm -rf $COMBINED_OUTPUT
mkdir $COMBINED_OUTPUT
node_modules/.bin/nyc -s node test1.js # leave off -s if you want to see partial results
cp .nyc_output/* $COMBINED_OUTPUT
node_modules/.bin/nyc -s node_modules/.bin/mocha test2.js
cp .nyc_output/* $COMBINED_OUTPUT
node_modules/.bin/nyc report -t $COMBINED_OUTPUT

每次调用nyc都会清除目录.nyc_output。但是,如果您将每个操作后的所有输出复制到另一个文件夹(我称之为nyc_output),因为每个文件都是使用唯一名称创建的,您可以使用所有覆盖范围让qa​​zxswpoi为您生成报告。文件。如果您使用nyc,它将不会打印该nyc操作的coverage表。

结果与其他答案相同

nyc -s
© www.soinside.com 2019 - 2024. All rights reserved.