unit testing for node.js - node.js

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 :)

Related

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

Comparison between `lab` and `code` vs `mocha` and `chai` nodejs libraries

I am new to node.js and I have decided that I will be using hapijs for my web and API implementations.
However, I have found that the hapi community built and use lab and code for the test and assertion libraries, which are a rewrite of mocha and chai.
I am having a hard time finding the differences between those libraries.
I know there is the possibility of using them all interchangeably but I would like a more detailed comparison, as I want to define and adhere coding standards and I don't like mix and match of libraries, unless there is a valid reason.
Any feedback is appreciated
When you are planning to build web application using nodejs Hapi is more suitable for it. Writing TTD is important for developer now a days. Lab and code is node framework to help to writing unit test in nodejs. Lab comes with Hapi to cover unit case for endpoint like writing test case for route and covering different scenarios for route. Code is an assertion library. Code is used if you are asserting response code ,response body and any header parameters.

Unit testing C++ v8 addon

We are writing a v8 addon for node.js.
The addon (as you may well know) is a C++ dll.
How do we unit test this?
The simple way is to use node.js scripts to call our exported functions from the addon dll. But this is not a true unit test, as it's across languages.
Has anyone managed to successfully write a C++ unit test for their addon dll?
We have tried, but are getting unexpected errors - we suspect this is because the node-gyp libraries expect everything to be called in the context of node.exe, and because our unit test uses the addon dll "standalone", some things are not getting setup correctly, causing the test to fall over.
If you have managed to use C++ unit tests for v8 addon, please can you detail the best way to do it, and things to look out for?
regards,
Stretch
I was thinking about this, too. But unless you have really lot's of cpp-logic it's perfectly fine to just write cpp-js-combination and unit test the js-implementation as can be observed in the Nan-library, here. Having less logic means here, that the cpp-implementation in the best case is anyhow just the API glue.
When you do have custom logic and write Nan#2-style classes (so, straightforward cpp) you can of course test it the like regular cpp by including it's headers and do some...
void testEquality()
{
CPPUNIT_ASSERT( /* some test*/ );
}

Need Testing in node.js

I'm quite new to testing in javascript, where I work, we us node.js for our projects.
I finished one of our projects, but I only used manual testing.
I need to develop my testing skill and I dont know how to improve it.
I need any testing tool to run my node.js projects, could you please tell me how to improve and learn new testing skills?
Look at Mocha test framework. I think this is the best choose for testing node.js applications at this moment.
If you are using Node to develop a web application you could use one of the headless browsers to test the user interaction (along with ajax) there are PhantomJS and Zombie for example.
Look around in Node Toolbox you'll find many frameworks and utilities to create your tests.
This is a good overview on some of the different unit test tools for Node.js:
3 Quick Tips for Writing Tests in Node.Js (after some rambling)

Resources