oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.01k stars 2.66k forks source link

Coverage - provide a way to exclude lines or branches from coverage #7662

Open keverw opened 8 months ago

keverw commented 8 months ago

What is the problem this feature would solve?

Sometimes might be edge cases or rare that you might want to exclude or delegated to helper functions. For example I'm making a little CLI tool and made a very minimalist logger utility, process.exit I'd never actually call. So in my tests I just overwrote the .exit() method.

Screenshot 2023-12-14 at 1 14 37 PM
bun test v1.0.17 (5e60861c)

logger.test.ts:
✓ Logger should log an error in red [1.36ms]
✓ Logger should log info in white [0.07ms]
✓ Logger should log a warning in yellow [0.05ms]
✓ Logger should log success in green [0.05ms]
✓ Logger should log a note in blue [0.06ms]
✓ Logger should log correctly and handle exit [0.12ms]
-----------|---------|---------|-------------------
File       | % Funcs | % Lines | Uncovered Line #s
-----------|---------|---------|-------------------
All files  |   71.43 |   92.86 |
 index.ts  |   71.43 |   92.86 | 5
-----------|---------|---------|-------------------

What is the feature you are proposing to solve the problem?

A way to add a comment similar to Istanbul.

For example:

/ istanbul ignore if /: ignores the next if statement. / istanbul ignore else /: ignores the else portion of an if statement. / istanbul ignore next /: ignores the next thing in the source-code ( functions, if statements, classes, you name it). So for example it can ignore the entire tree possibly. / istanbul ignore file /: ignores an entire source-file (this should be placed at the top of the file). / istanbul ignore else|if /: Ignores a “else if” - but it has to be at the top of the tree:

What alternatives have you considered?

Not really any ideas, my plan is to just keep working on my project and if there's a better way to do this, it'd be great otherwise something I can live with.

elringus commented 8 months ago

This is actually an essential feature for a production-ready test/coverage system, and one of the blockers (others being https://github.com/oven-sh/bun/issues/4015, https://github.com/oven-sh/bun/issues/7100) stopping me from switching to bun test.

Other popular test frameworks have this from v1, eg in vitest: https://vitest.dev/guide/coverage.html#ignoring-code (similar works in jest).

Hopefully this could be added to the roadmap. @Electroid

jtenner commented 4 months ago

I just hit this problem too. For now, I have to reduce test coverage size, because what I'm testing dynamically creates modules using a BunPlugin.

It would be nice to be able to skip an entire module.

roninjin10 commented 1 month ago

I'm migrating https://github.com/evmts/tevm-monorepo to vitest in future because of this issue

Jarred-Sumner commented 1 month ago

we can add a way to do this, particularly if the first implementation is just the next line

MKRhere commented 1 month ago

@Jarred-Sumner That won't do, I can't do an ignore for each line in a const enum that's bringing down coverage...

I would prefer to have something like /* coverage ignore start */ and /* coverage ignore end */.

image