Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sylvainhalle opened this issue Jan 27, 2021 · 0 comments
Open
Labels

Comments

@sylvainhalle
Copy link

  • Version: 15.5.1
  • Platform: Linux Symphorien 4.15.0-132-generic Deprecated Buffer usage in dependency #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 sylvainhalle changed the title Line with throw not marked as covered if caught by mocha assertion Line with throw not marked as covered if caught by chai assertion Jan 27, 2021
@bcoe bcoe added the bug label Feb 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants