Node.js Code Coverage of Certain Test Files - javascript

I just came into a Node.js project that has a ton of unit tests, most of which, unfortunately, are outdated and don't run. There's no test runner or any kind of code coverage tool implemented, and it is certainly not in my scope of work to refactor everything.
That being said, that doesn't stop me from wanting to check the code coverage of my work. I've looked at both Istanbul and Blanket and can't find a way to only have them run certain unit tests. Surely, there is a way so that I can get a report that, obviously, reports a very low percentage of coverage, but that I can dive into certain files and such (those that my tests hit) to ensure my code is throughly tested.

With istanbul.js, you can easily get the coverage information this by for example specifying the following command (Following example uses mocha but it will be similar for any test framework):
istanbul cover node_modules/.bin/_mocha -- -u exports -R spec test/test1.spec.js test/test2.spec.js
You can also specify all the tests in particular sub-directory eg: test/yourfeaturetest/*.spec.js. You also have something like test/myfeature/**/*.spec.js to cover all the tests in the test/myfeature directory including tests that could have been created recursively in sub-directories.
As for me, I use gulp and thus utilize plugins such as gulp-istanbul and run tests and coverage via gulp tasks.

Related

The same file's coverage of entire project not equal to run itself only

I've publish a tool which based on jest to test my code and collect coverage.
I found the same file's coverage in entire coverage report can't match single one file coverage report since I use v8 coverage provider.
This is my coverage of entire project.
It can't match the result that I run jest in single one file.
I think it maybe v8 coverage problem and also try out babel coverage, It seems OK.
So, the problems is that I can't remember why I use v8 instead of babel becuase leave on a warnning in my tool.
Is there any problems if I switch v8 to babel?

Is there a straightforward way in Jest to specify an array/list of tests to run?

Problem
I'm writing several test suites (using Jest and Puppeteer) to automate tests of my AngularJS app's home page. At this point, I have nearly 70 tests in one of my test files (aka test suite). And I'm wondering if it's possible to pass an array of test names to Jest's -t option to run a specific subset of tests in one of my test files. This will be helpful for debugging, as I have some tests that are interdependent and thus always need to be run together, but it would be nice to avoid having to run the entire test suite (which runs in about a minute) every time I want to debug a few tests.
Overview of my testing environment:
Puppeteer version: 1.19.0
Jest version: 24.8.0
Jest commands I've tried
I've been reading the Jest docs and so far I've come across a few CLI options that have the potential to solve this problem, but have not been fruitful so far.
jest name-of-test-file.spec.js: This runs all the tests in a single test file, but I don't want to do that, I just want to run a specific subset of tests in a test file.
jest name-of-test-file.spec.js -t name-of-test: This only runs a single test in a test file, but I want to run a few different interdependent tests.
jest name-of-test-file.spec.js --testPathPattern=<regex>: This runs all the tests whose names match the regex pattern I pass. It could be helpful but so far I haven't been able to come up with a valid regex that matches against three unique strings (i.e. test names). Unfortunately my test names differ greatly so it would be hard for me to come up with a more generalized regex to match the specific tests I want to run.
Final thoughts/ question:
I think option 3 has the most potential, but I haven't been able to find a working regex.
If the regex option does not work for you, you can use it.only (or test.only) to only run specific tests inside a module.
Quote from the docs:
When you are debugging a large codebase, you will often only want to run a subset of tests. You can use .only to specify which tests are the only ones you want to run.
Code Sample
it.only('This test will run', /*... */);
it.only('This test will also run', /*... */);
it('This test will not run', /*... */);
However, you still need to specify which module or file to run in case you are having multiple test files:
jest name-of-test-file-to-run.spec.js

Run Jasmine tests from WebStorm

WebStorm shows icons next to Jasmine's test cases and it seems as if there should be an option to run it, but the menu just says: Nothing here.
As I did not find any documentation about this feature, I wonder if it should be possible to run tests like this and if yes, what the conditions are.
This likely means that no suitable test runners have been found. WebStorm doesn't manage test running directly. This job is done by a test runner. WebStorm supports several test runners - Mocha, Karma, Jest, JsTestDriver, nodeunit,... The logic used for determining what test runner is available for a given test file is based on dependencies declarations in nearest package.json file.

Unit Testing - test for file count and folder size

I have homework JS code to check every week.
I have tests for JS logic, but i also has a requirement for overall size and file quantity.
So i was wondering is there any possibility to write tests (i assume mocha - because it runs on node, and has access to FS) for folder size and file count in folder (also has a limit)?
I have no experience in Mocha or Node, so should i dive deeper?
I'm not quiet sure what you are asking, but:
Mocha is a framework to run "unit tests" to test the software you have written and see if it's working ok.
If you just want to count some files in a directory you don't need a testing framework. Just use node's normal filesystem classes in fs https://nodejs.org/api/fs.html to write your code.
If you want to check that files maybe you are talking about integration test and not unit test. So, I believe that if you want to make unit test you will need read something about mock's and stub's. You can search modules like sinon. A few weeks I was fighting with test that checks if dirs or files are created and I reached the conclusion that if I want to make unit tests I need that module. Here's Sinon

too high Code Coverage in KarmaJS with karma-coverage & Jasmine

I'm using Jasmine as the testing framework for my AngularJS application. I run the tests with the help of Grunt & KarmaJS. KarmaJS also generates the code coverage with the help of karma-coverage.
Now I've created a model for configuaration data, which I also have to instantiate for other tests. Because of this instantiation I get a code coverage for this file although I haven't done any tests for it. Only because while the test run all of the lines were used, the coverage is 100%.
Now the question: Is there a way to specify in my tests which files they cover?
In PHP Unit there is an #covers annotation which specifies what code is covered with the test.
Thx
Since karma-coverage uses Istanbul under the hood, all configuration for Istanbul should work for karma-coverage.
In Istanbul, you can specify that a block of code be ignored for coverage purposes. You can try placing something like this at the top of your file:
/* istanbul ignore next */
I haven't tried this myself, but I'd bet that this or something similar would do what you want it to do.

Categories