Turn your manual testers into automation experts Request a DemoStart testRigor Free

Express.js Testing

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.

To form a solid test strategy for an Express-based application, you ought to use the following structure:

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();
  });
});

Here, you can see that since Mocha supports BDD and TDD way of scenario descriptions, it supports two main methods, namely:
  • 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.
You can choose from the following methods when it comes to assertions and validations. You can read more about them over here.
  • 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.
During setup, be sure to update the package.json file. Update the "scripts" block to "Mocha," as shown below.

{
  "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.

You need to update the package.json file, as mentioned above, with Jest for Mocha. Jest uses describe() and it() methods along with its own vocabulary for validations, assertions, and helpful methods such as:

When working with these tools and frameworks, remember to install them as dev dependencies.

Integration Testing

Integration tests are meant to validate if two or more components work together correctly. With a backend framework like Express, you need to ensure that server-side calls are happening as per expectation. Above mentioned tools, like Mocha and Jest, can help you with this. Some scenarios where integration testing can come in handy are as follows:
  1. Checking routes for different kinds of responses. For example, a 404 is logged when an incorrect parameter is being passed.
  2. GET and POST call endpoints.
  3. Testing interactions between middleware, services, and controllers.
  4. 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.

You can create automated end-to-end tests using the following ways:
  • 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

When it comes to Express.js end-to-end testing, it's hard to beat testRigor for its ease of use, speed of test creation, and minimal maintenance. testRigor is a powerful no-code automation tool, enabling the entire QA team (including manual testers) to create robust E2E tests fast. Here are some of the benefits of using it for your testing needs:
  1. Supports cross-platform and cross-browser testing without significant configuration steps.
  2. Test creation is up to 15X faster than Selenium-based tools.
  3. Test maintenance is practically eliminated and takes very little time.
  4. Has provision to support 2FA testing support for Gmail, text messages, and Google authenticator.
  5. Easy integration with most CI/CD tools.
  6. Facilitates easy mocking of API calls and accessing databases if needed.
  7. 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.

Join the next wave of functional testing now.
A testRigor specialist will walk you through our platform with a custom demo.