Javascript Testing Code Coverage: Moving from Istanbul to NYC
Moving to NYC or Istanbul both cities are just amazing 😂 but away from the sad joke, Code coverage is essential for reporting and just for checking your overall code status when it comes to testing your code base.
Sadly enough Istanbul is pushing us to move to NYC especially if your codebase is using the latest ES* features, one, for instance, are Arrow Functions which apparently Istanbul hates 😔 and will show you something similar to this :
Failed to parse file: /Users/macbook/Desktop/project/file.js
Transformation error; return original code
{ Error: Line 4555: Unexpected token =>
at constructError. // TONS OF AWFULL STACK
. // TONS OF AWFULL STACK
. // TONS OF AWFULL STACK
. // TONS OF AWFULL STACK
. // TONS OF AWFULL STACK
. // TONS OF AWFULL STACKat LOOP (fs.js:1745:14)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
lineNumber: 4555,
description: 'Unexpected token =>',
index: 83630 }
As you can, not that pretty and simply don’t make sense as an error, how come it doesn’t understand sweet Arrow Functions 🤯🤯🤯.
Now let’s just check our configuration inside our package.json file, typically you would have something similar to this :
Focus a bit on the test function, you will find we’re using Instabul cover feature to wrap our mocha test for instance, and you typically can add files that you want to exclude and of course you will need to mention the testing folder in case you have a custom testing folder.
Now, keep that one and go to your Terminal/CMD (you hacker boy/girl 😂) and type either of the following commands depending on your project usage to either good’ol NPM or YARN.
-> npm i --save-dev nyc
or
-> yarn add --dev nyc
Now, we the past line over package.json will know some changes, but first, to keep things clean, let’s create a new file that can be either a JSON/YAML/JS files to host our NYC configuration.
Typically I’m a .*rc kind of guys so, I will create a new file in my working directory with name .nycrc which will be a JSON file and its basic content will be similar to this one :
Let’s digest what we’ve added :
- include: will be either a file or an array of testing files, we want to test.
- exclude: will be either a file or an array of files we want to exclude from the testing.
NPM modules will be excluded out of the box, but in case you want to change that (for a reason or another 🤔) you can, by adding this :
"excludeNodeModules": false
Now, let’s create a new task in our package.json file inside the test clause, and let’s call it test_nyc to showcase the difference between the two versions:
As you can see, the configuration will take away most of the pain and no extras over our scripts clause.
To run this simple head back to your Terminal and simply write :
-> npm run test_nyc
or
-> yarn run text_nyc
And we’re done and now our ES6+ tests will be running without an issue 🎉
If you liked this article please feel free to check my other articles and follow me over Medium :
And Twitter :
Thank you in Advance 😇 !