Unit testing vs Integration testing of an Express.js app - node.js

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.

Related

Is it possible to generate the test coverage for a project using Jest which is tested with Chai JS?

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

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

API testing using protractor+jasmine

Does anybody using protractor with jasmine to do API testing. While searching for this I get to know that using frisby.js we can do API testing. But, my doubt is that whether protractor or jasmine directly supports/provides functions for API testing. Did anybody tried this? If so, what is the approach that I need to follow ?
Thanks in advance.
Protractor is meant for e2e testing and e2e tests are supposed to test the flow of an application from user standpoint, in spite of that you should test your API calls not directly but rather through testing user actions and if actions perform as intended it means the API that they rely on work.
If you want to do tests for API to catch errors early without having to run full e2e test suite you should use frisby.js as you've mentioned to confirm all APIs are A-OK and you can follow then with e2e tests when you are sure that all should be working.
IMO it's better to use the tools for what they were designed.

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.

unit testing for 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 :)

Resources