A Couple of Words about Coverage

Today we talk about a new coverage tool 🎩ESCover and much more!

coderaiser
CodeX
4 min readJan 31, 2022

--

Astride the Ox, I reach home.
I am serene. The Ox too can rest.
The dawn has come. In blissful repose,
Within my thatched dwelling
I have abandoned the whip and ropes.
© Zen Ten Bulls

Hi guys!

I think you know that code transformer 🐊Putout has 100% test coverage. Why so? The thing is writing parsers is a very hard task, but worry not! There is a helper 😏!

Mock the import!

Mock-import exists for about a year and used to mock things when you using EcmaScript Modules. It uses 🐊Putout and changes ImportDeclarations to VariableDeclaration when you need it the most. So such code:

Becomes:

It works amazing! But there is a problem with coverage:

Taking into accord these two factors.

🧨 New players in the game get you covered!

One more coverage tool comes into the wild: 🎩 ESCover. It's also based on 🐊Putout . It sets markers that will be called later, changing this code:

Into this:

__c4 is a variable with a special meaning: it provides a function that gives information about line and number of current instruction.
Here is what the report looks like when everything is fine:

When something isn’t right:

Or like this:

You can choose what you prefer 😏.

🎩ESCover is ready to be used on a regular basis 🎉!
The only thing is: it works with EcmaScript Modules only. And this is for a reason: for CommonJS you can use c8 and nyc. But there is no tool that shows coverage for files changed with node loaders.

Results of 🎩ESCover are saved to coverage/lcov.info and it reads them when done. A couple of things about coverage:

-☝️ lcov was created in 2002, almost twenty years ago.
-☝️ Linux kernel developers created it to know what is going on with the coverage.
-☝️ lcov originally was written in PERL and has text-based format.
-☝️ This is most popular coverage format of all times supported by a lot of tools (like coveralls, c8 and nyc).

So if we run our ESM application with:

We will receive coverage/lconf.info with something similar to:

Where:

  • SF - is the path to source;
  • DA - is line number, and count of running;
  • end_of_record latest record for current file entry;

As for me, this is really human readable, and very compact form. The only thing I don’t get for now is: counters, I see no reason why do I need them 🤷‍♂️, if you have a reason create an issue. It can be added in one line of code.

🎩ESCover on early stages of development, so a lot of things can change, but it is already used on a couple of projects, and everything is fine 🎈.

🤷‍♂️ What more can I do with 🐊Putout?

You can trace the execution of functions with the help of ESTrace. It provides such information:

And does the next transformations. Let’s suppose that you have a function:

const fn = (a) => a

EStrace will replace it with:

🤷‍♂️ What with new rules?

@putout/plugin-convert-commonjs-to-esm doesn't require you to usesimport anymore. Only built-in modules 💪 (Thanks to @putout/plugin-declare-undefined-variables which supports native node.js modules, and will declare it, when you use it 😉).

@putout/plugin-nodejs got a new rule: convert-dirname-to-url. What does it do?

Changes:

To:

This will help to get rid of __dirname in EcmaScript Modules. Here is the fast draft and results.

🦔 Real world uses

I just found out (I knew that they were going to, but who knows 😅) that folks from Stylelint used 🐊 Putout to convert their codebase to ESM 🎉.

And got a little problem with jest: it doesn't recognize import.meta.url 🤷‍♂️. Well, that’s sad 😕, but hey!

This is why 📼 Supertape exists: simplest high-speed testing.
Thanks a lot to the Stylelint team! That's awesome what you are doing and very inspiring!

That’s all for today! Have a nice coding 🎮!

More content at plainenglish.io. Sign up for our free weekly newsletter. Get exclusive access to writing opportunities and advice in our community Discord.

--

--

coderaiser
CodeX

Open Source contributor, maintainer and developer. Author of a lot of npm packages.