node-qunit, code coverage tool - node.js

For nodejs backend server code Unit Testing, I am using node-qunit with grunt.
Is there any code coverage tool using node-qunit module?
Maximum code coverage tool I am seeing needs headless browser support, ex. PhantomJS, but if I run using this, then I get syntax errors for nodejs keywords, like "ReferenceError: Can't find variable: require" etc.
So which tool I can use for code coverage for nodejs backend code testing using node-qunit.

If you're solely testing backend code, there's no need to run the tests in a headless browser like PhantomJS. For running code coverage analysis in node, I can recommend istanbul.
But I'm not sure if it works out of the box with node-qunit. However mocha is a popular node.js test runner with a qunit-interface, and qunit-mocha-ui delivers QUnit's assertions for mocha. So you could migrate your tests with only little effort.

Related

how to get code coverage when running tests written in tavern with pytest, having nodejs app running in backend?

I have an API written in node js, and integration tests for endpoints written in tavern with pytest. I want to get code coverage of those integration tests after running. For now, the setup is; first starting node app, and running tavern test suites with pytest command.
See lots of scenarios but all have a consistency in btw test engine and app engine e.g python - python or js - js. How could I get code coverage of tavern tests suites w.r.t node app?
-UPDATE-
The API written in node js -v12.16.2- with nest js framework, we run app with nest start, and running tests written with tavern -v0.34.0- with pytest -v4.5.0- on the other tab against running api. And I want to learn how to get code coverage of app by being hit endpoints by test requests by tavern.
You have 2 programs here:
The nodejs app
The tavern test suite
You're interested in finding the coverage of the nodejs part, which means you need to instrument that program.
I've only had a quick look around, but it would appear that using https://github.com/istanbuljs/nyc is a good bet, meaning you can just run your server with nyc prepended to measure the coverage when you run the tavern tests.
Note: this is a vague answer, I will update if the question is made more specific.

Node.js test coverage

Could anyone, please, recommend simple test coverage tool for Node.js without using Mocha because I use simple functions from assert module of Node in my own test framework.
The standard library for code coverage is nyc, so that's my suggestion; however, you aren't going to get code coverage to work with a test framework you came up with yourself for free -- you'll need to make sure you are instrumenting the appropriate source files so the report contains what you expect.
The following links, which discuss non-standard uses of nyc (non-standard meaning, not jasmine or mocha) might be useful - one for classic istanbul, one for the newer nyc:
https://github.com/gotwarlost/istanbul/issues/574
https://github.com/istanbuljs/nyc/issues/548
You can use Jest and Supertest.
Jest -> https://jestjs.io/
SuperTest -> https://www.npmjs.com/package/supertest

Can Jest be used as browser based testing tool?

I understand that Jest is a unit testing tool for developers used for JavaScript. Is Jest a browser based testing tool similar to Selenium or a functional testing tool?
As you mention, Jest is meant to be a unit testing tool. Normally you'd write small tests for parts/components of a web-page. I'm not exactly sure what you mean by "Is Jest can be used as Browser based Testing tool?" but I've found there are two relevant areas where Jest can come into contact with browser based testing
You can use a virtual DOM (like JSDOM) to render your components and test them in an environment similar to a browser. These are still unit tests but you'll have access to window and document and can test things like document click, window navigation, focused element etc.
You can debug your Jest tests in browser. Follow the instructions here if that is what you want. I've tried this but it was really slow and not very useful for me so I wouldn't recommend it
You can probably render your entire application and test it with Jest, but I wouldn't recommend that either. Jest tests should be designed to run fast and should only tests small units of your code. If you try and build tests that take a long time to run then there is an argument stating that your unit tests will become useless and developers will eventually not run them anymore.
If you are looking for tests that start an actual browser and click around like a user then have a look at Selenium which I would think is the most common approach these days
This npm library can be integrated with your jest tests to run them in a browser :) :
https://www.npmjs.com/package/jest-browser
I can't say how good it is/what the cons are but it looks like it is worth a try!
Yes, you can use Jest Preview (https://github.com/nvh95/jest-preview) to debug your Jest test in a browser like Google Chrome.
You don't have to debug a long HTML text when using Jest Preview anymore.
Read more at https://www.jest-preview.com/docs/getting-started/intro

What is the difference between mocha and Selenium?

I started using Node.js and was looking for a testing framework.
I found :
Mocha
Selenium
I understand that with Mocha one could to write tests in JS while with Selenium, one has to write tests with lower level languages like C#.
Apart from that is there something Selenium can do that Mocha can't?
What use does Mocha have by itself?
Mocha and Selenium both deal with testing software but they solve different problems.
Mocha is test running framework. You tell Mocha what tests you have and what tests you want to run and mocha will run your tests and report those that passed and those that failed. Mocha by itself provides a test running framework. You'll typically want to use an assertion library with it, like Chai. I have test suites where the only libraries providing testing support are Mocha together with Chai. This is a perfectly viable use-case.
Selenium is a library for controlling browsers. A major part of its scope is testing browser-based software. However, it can also be used for scraping web sites. This is something that Selenium can do that Mocha cannot do, by itself. Conversely Selenium is not a test running framework. Selenium has no facilities dedicated to delimiting tests and running only specific tests. You have to rely on a test running framework like Mocha to delimit one test from another.
If you want to write a test suite that tests a browser-based application you could use Mocha together with Selenium. Or Jasmine (another test running framework) with Selenium. Or you could use Behave (a Python based test runner) together with Selenium. Or you could use Mocha together with some other library that controls browsers.
This specific question needs special treatment:
I understand that with Mocha one could to write tests in JS while with Selenium, one has to write tests with lower level languages like C#.
I would not call C# a lower-level language. At any rate, using Mocha you'd have to use JavaScript. (There's a testing library for Ruby also named "Mocha" but which is not a Ruby version of the JavaScript one. I'm assuming you are talking about the JavaScript one, which makes my response a tautological but here we are.) You can use Selenium with JavaScript, Python, C#, Java and a bunch of other languages.

Node.js and Mocha Tests from the Browser

This is probably a really dumb question, but I'm not sure. If I have a node.js application and a much of tests built with Mocha, I can run them on my server no problems, using mocha. However, is there any way to invoke the unit tests through a browser and just pipe the results to the page? For development, I'd like to have a page on my website that shows the results of the tests.
Every time I search for how to do this, it's testing the actual browser code I'm finding (the dom, the UI etc), but it's actually just that I want to see the results of the tests run server-side but from the browser!
Thanks in advance!
Sure, create a route in Express that executes mocha and returns the result to the browser. Take a look at child_process.fork to execute mocha: http://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options
And don't forget to turn it off before going into production!
Separately, I would highly recommend to you the concept of continuous deployment, and specifically the service CircleCI, which for $19 automatically runs all of your tests every time you push to Github.

Resources