我有一个在代码中发生特定事件后过滤和执行的函数列表。所有函数都被评分并导出为如下所示的数组
const interceptors = [
{
recipeName: 'Test1',
vendor: 'aaa',
os: 'bbb',
completed(name, output) {
const tableText = output.getCommandText('test list');
const parser = new TableParser(tableText);
const list = parser.getBody();
Device.updateOne({ name }, { $set: { list } }, done);
},
},
{
recipeName: 'Test2',
vendor: 'aaa',
os: 'bbb',
completed(name, output) {
const users = [];
const text = output.output;
const matches = matchAll(text, /^user (\w+)/gm) || [];
matches.forEach((m) => {
users.push({ username: m[1] });
});
Device.updateOne({ name }, { $set: { users } }, done);
},
},
];
module.exports = interceptors;
上面代码相关的测试文件可以在下面做种子。基本上,它加载拦截器数组并对其进行过滤以查找相关操作并执行。所有相关测试均已通过,但覆盖率报告中未显示任何内容。
const fs = require('fs');
const path = require('path');
const chai = require('chai');
const sinonChai = require('sinon-chai');
const sinon = require('sinon');
const { assert } = chai;
const interceptors = require('../../../core/recipes/test1/aaa-bb/interceptors');
const Device = require('../../../models/device');
require('sinon-mongoose');
chai.use(sinonChai);
it('show user', () => {
const name = 'TestDevice';
const stub = sinon.stub(Device, 'updateOne');
const interceptor = interceptors.filter(
f => f.recipeName === 'Test2',
)[0];
const output = getTestData('data1.txt');
const parser = new TerminalOutputParser(output);
interceptor.completed('TestDevice', parser);
const checkObject = sinon.match((users) => {
assert.equal('cem', users[0].username);
assert.equal('testuser', users[1].username);
return true;
});
sinon.assert.calledWith(
stub,
sinon.match.has('name', name),
sinon.match.has('$set', sinon.match.has('users', checkObject)),
);
Device.updateOne.restore();
});
});
有没有办法显示测试中执行的功能的覆盖率?