bcoe / bcoe/c8

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

Open
#282 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
JavaScript
Stars
2.1k
Forks
99
PR merge metrics
No merged PRs in 30d

Description

* **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`):

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

export {f};
```

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

```javascript
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:

```javascript
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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.