c8 icon indicating copy to clipboard operation
c8 copied to clipboard

Line with throw not marked as covered if caught by chai assertion

Open sylvainhalle opened this issue 4 years ago • 0 comments

  • Version: 15.5.1
  • Platform: Linux Symphorien 4.15.0-132-generic #136-Ubuntu SMP Tue Jan 12 14:58:42 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
  • c8 version: 7.4.0, run with extension "mjs"
  • mocha version: 8.2.1
  • chai version: 4.2.0

It seems that the way the chai assertion library traps exceptions that are thrown causes c8 not to mark the executed lines as covered. I don't know if the issue is caused by c8 or by chai, though.

Steps to reproduce

Consider the following source file (index.mjs):

function f(x)
{
    if (x == 0)
    {
        throw "foo";
    }
}

export {f};

And the following chai test case (test/test.test.js):

import pkg_chai from "chai";
const { expect } = pkg_chai;

import {f} from "../index.mjs";

it("Test 1", () => {
    expect(() => {f(0);}).to.throw;
});

Expected outcome

  • The test passes (since the exception is thrown)
  • Line 5 in index.mjs is marked as covered

Observed outcome

  • The test passes
  • Line 5 is not marked as covered

Additional note

If I replace the test case with this alternate version:

import pkg_chai from "chai";
const { expect } = pkg_chai;

import {f} from "../index.mjs";

it("Test 1", () => {
    var thrown = false;
    try {
      f(0);
    }
    catch(e) {
      thrown = true;
    }
    expect(thrown).to.be.true;
});

...this time all lines in index.mjs are marked as covered.

sylvainhalle avatar Jan 27 '21 15:01 sylvainhalle