Express.js Testing
Express.js (also known as Express) is a framework that aids in working with Node.js. It is used for server-side development, such as creating middleware, routing, templating, and easy debugging. This framework can help you build your web and mobile applications with ease.
Unit Testing
Unit testing plays a crucial part in ensuring the application's quality. These tests are usually faster and less expensive in comparison to integration and end-to-end testing methods. For writing your test cases, you will need a framework that allows you to run the tests and an assertion library that will help validate outcomes. Though many options are available, listed below are some of the most popular tools.
Mocha: One of the most popular Express testing frameworks used for writing unit tests. You can get asynchronous execution and detailed reporting of your test cases through this framework. Pairing it with another assertion library can improve the quality of unit test cases. You can find the assertion library options that pair well with Mocha over here.
Chai is one such assertion library that can be paired with Mocha to write powerful unit test cases. It supports node BDD/TDD assertions. For example, when you execute 'mocha sellerDetails.js' in the command line, the sellerDetails.js file, which is a javascript file, will get executed. This file will contain the tests that have been written using special features and assertions that are available as a part of Mocha.
Below is an example of a unit test using Mocha and Chai where the test case is expected to verify if the code is calculating the volume and surface area of the cube.
const Cube = require('../src/app').Cube; const expect = require('chai').expect; describe('Testing the Cube Functions', function() { it('1. The side length of the Cube', function(done) { let c1 = new Cube(2); expect(c1.getSideLength()).to.equal(2); done(); }); it('2. The surface area of the Cube', function(done) { let c2 = new Cube(5); expect(c2.getSurfaceArea()).to.equal(150); done(); }); it('3. The volume of the Cube', function(done) { let c3 = new Cube(7); expect(c3.getVolume()).to.equal(343); done(); }); });
- describe() - A suite of test cases with a string and a function as parameters.
- it() - It is the smallest unit test case. Multiple it() methods can be grouped inside a describe() method.
- expect() - A BDD-style library mainly used with booleans and numbers.
- should() - Another BDD-style library that extends each object with a should property to start the chain.
- assert() - This is a TDD-style library with additional tests available.
{ "name": "chai", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "mocha" } }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "chai": "^4.3.6", "mocha": "^9.2.2", } }
Jest: Another popular framework often used for node testing, Jest is an open-source framework built over Jasmine. You can use Jest with other Node libraries like SuperTest, which can be used to test HTTP servers to get high-quality unit tests. Jest also supports CLI tools that can give you more features and ease of execution.
When working with these tools and frameworks, remember to install them as dev dependencies.
Integration Testing
- Checking routes for different kinds of responses. For example, a 404 is logged when an incorrect parameter is being passed.
- GET and POST call endpoints.
- Testing interactions between middleware, services, and controllers.
- Verifying that data is correctly fetched from and written to databases through API calls.
Not all integrations require integration test cases. If you are using third-party applications like Express framework or MongoDB, you don't need to test those portions of code that are provided as a part of these frameworks - as they've already been tested. You need to focus on those integrations where you have written custom logic and how these custom portions of code integrate with the framework's methods.
Although integration tests are lightweight in comparison to end-to-end tests, they can still be time-consuming if the entire system is booted. In this case, it's essential to balance between real-life data and the data or components that are stubbed.
End-to-End Testing
End-to-end testing involves verifying the entire workflow and is the most encompassing type of testing. Therefore it is generally done after both unit and integration tests pass for a given build.
- Selenium-based tools
- testRigor
Selenium-based tools for end-to-end testing
There are many such tools available that help with end-to-end testing. Most of them have wrappers over Selenium. However, most of these tools also inherit the limitations of Selenium to a certain degree.
testRigor for end-to-end testing
- Supports cross-platform and cross-browser testing without significant configuration steps.
- Test creation is up to 15X faster than Selenium-based tools.
- Test maintenance is practically eliminated and takes very little time.
- Has provision to support 2FA testing support for Gmail, text messages, and Google authenticator.
- Easy integration with most CI/CD tools.
- Facilitates easy mocking of API calls and accessing databases if needed.
- It's cloud-hosted and highly customizable, allowing for broad configuration types and speed of test execution.
Below is an example of an automated test case for a user management system:
Login Click on “Menu” to the right of “Announcements” Click on “User Management” Check that page contains “User Management” Click on “Manage Users” roughly below “User Preferences” Click on “create new user” at the top Enter stored value “full name” into “Name” Enter stored value “newUserEmail” Click on “Save” Scroll down up to 5 times until page contains stored value “new user” roughly below “Username” check that email to stored value "newUserEmail" and “Welcome” in subject was delivered
Conclusion
Having a series of tests described above will help you deliver high-quality applications in the market. There are many options available for you to choose from when it comes to testing frameworks. However, considering your requirements will be the best step to picking a set of tools that work best for you.