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
Related
I have a project which has various unit tests running successfully using chai js.
Is it possible to use Jest and generate the coverage for this project without rewriting unit tests with Jest JS?
Please provide any documentation to support
It is not possible to do so without rewriting unit tests with Jest JS.
You can search for plugins otherwise
It isn't because Jest has no way to understand your Chai tests.
You will have to migrate them to Jest. As their interfaces are pretty much similar it will not be a painful task.
Airbnb details why and how they do it from Mocha to Jest on a large codebase in this Medium post.
It is possible to migrate from chai using jest-codemods. However you might have to rewrite some configurations and tests(for version compatibility). You can automate changes to your codebase through this.
Please go through these links for further details.
https://jestjs.io/docs/en/migration-guide
https://github.com/skovhus/jest-codemods
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.
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.
I looked into various frameworks for writing unit tests for an application developed in node.js. There exists multiple options like: nodeunit, jasmine-node, should.js library in Mocha. All seems to be pretty much capable of testing everything. I couldn't find any limitation of any of above mentioned options.
I will prefer to use nodeunit as it seems easy to use as a beginner. Any suggestion about any limitation of nodeunit would be highly helpful before I start working on this. Or any suggestion if anyone thinks that there exists a easier and better option for unit testing in node.js.
I previously used JUnit in Java and want to find similar testing framework. It was hard for me to getting started with BDD frameworks like Mocha. Finally I stoped on node unit. All advanced functions which I couldn't find in node unit was finded in expect.js. Also important testing framework in web application context is superagent(which helps to make requests, and get responce on tests), it was easy to mix with node unit, and easy to test async code. One more nice thing there is plugin for WebStorm :)
I'm writing tests for an Express.js app and I don't know how to choose between unit tests and integration tests.
currently I experimented with:
unit tests - using Sinon for stubs/mocks/spies and Injects for dependency injection to modules. with this approach I have to stub MongoDB and other external methods.
I thought about unit testing the individual routes and then using an integration test to verify that the correct routes are actually invoked.
integration tests - using Supertest and Superagent, much less code to write (no need to mock/stub anything) but a test environment should exist (databases, etc..)
I'm using Mocha to run both styles of tests.
how should I choose between those two different approaches ?
You should probably do both. Unit test each non-helper method that does non-trivial work. Run the whole thing through a few integration tests. If you find yourself having to do tons and tons and tons of mocks and stubs, it's probably a sign to refactor.