0

I have a list of functions that are filtered and executed after a specific event occurs in the code. All functions are scored and exported as an array like below

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;    

And the test file related above code can be seed below. Basically it loads the interceptors array and filters it to find related action and executes. All related tests are passed but nothing is shown in the coverage report.

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();
  });
});

Is there a way to show coverage for functions executed in a test?

caltuntas
  • 10,747
  • 7
  • 34
  • 39

1 Answers1

0

In case someone else faces similar problem , I'm posting my solution

I discovered it was a configuration problem of mocha library. By default mocha looks for the test files under "test" directory but in my case above test file was under sub-directory so it is not picked by mocha.

I changed configuration of mocha

from "test": "mocha --reporter dot", to "test": "mocha test/**/*.js --reporter dot"

and problem is solved.

caltuntas
  • 10,747
  • 7
  • 34
  • 39