Shoulder.dev Logo Shoulder.dev

Writing Tests for benhall/express-demo

Scenario:

You have built a web application using benhall/express-demo, and you want to ensure its reliability and maintainability. To achieve this, you will write tests using a testing framework. In this example, we will use Mocha and Chai for unit and integration tests, and Puppeteer for end-to-end tests.

Solution:

  1. Install the required dependencies:

In your terminal, run the following command to install Mocha, Chai, and Puppeteer:

npm install mocha chai chai-http puppeteer --save-dev
  1. Create a test directory and a mocha.json file inside it:
mkdir test
touch test/mocha.json
  1. Configure mocha.json:

Edit the mocha.json file and add the following content:

{
"ui": "bdd",
"reporter": "spec",
"require": [
"chai",
"chai-http",
"./index.test.js",
"./users.test.js"
]
}
  1. Create test files for routes:

Create two test files, index.test.js and users.test.js, inside the test directory.

  1. Write unit tests:

In index.test.js, write tests for the index.js route. For example, test if the root route returns the correct status code and response.

const chai = require('chai');
const chaiHttp = require('chai-http');
const server = require('../app');

const { expect } = chai;
chai.use(chaiHttp);

describe('GET /', () => {
it('it should return status code 200', (done) => {
chai.request(server)
.get('/')
.end((err, res) => {
expect(res).to.have.status(200);
done();
});
});

it('it should return the correct response', (done) => {
chai.request(server)
.get('/')
.then((res) => {
expect(res.text).to.include('Welcome to benhall/express-demo!');
done();
})
.catch((err) => done(err));
});
});
  1. Write integration tests:

In users.test.js, write tests for the users.js route. For example, test if the /users route returns the correct status code and response.

const chai = require('chai');
const chaiHttp = require('chai-http');
const server = require('../app');

const { expect } = chai;
chai.use(chaiHttp);

describe('GET /users', () => {
it('it should return status code 200', (done) => {
chai.request(server)
.get('/users')
.end((err, res) => {
expect(res).to.have.status(200);
done();
});
});

it('it should return the correct response', (done) => {
chai.request(server)
.get('/users')
.then((res) => {
expect(res.body).to.be.an('array');
expect(res.body[0]).to.have.property('name');
done();
})
.catch((err) => done(err));
});
});
  1. Write end-to-end tests:

Create a new file, e2e.test.js, inside the test directory. Write tests using Puppeteer to simulate user interactions and check the application’s response.

const puppeteer = require('puppeteer');
const chai = require('chai');
const expect = chai.expect;

describe('End-to-End Tests', async () => {
let browser, page;

beforeAll(async () => {
browser = await puppeteer.launch();
page = await browser.newPage();
});

afterAll(async () => {
await browser.close();
});

it('should visit the root route', async () => {
await page.goto('http://localhost:3000');
const text = await page.textContent('body');
expect(text).to.include('Welcome to benhall/express-demo!');
});

it('should visit the /users route', async () => {
await page.goto('http://localhost:3000/users');
const text = await page.textContent('body');
expect(text).to.include('Users');
});
});
  1. Run the tests:

In your terminal, run the following command to execute all tests:

npm test

Tests:

  1. Unit tests for the index.js route.
  2. Integration tests for the users.js route.
  3. End-to-end tests for the application.

These tests will help ensure the reliability and maintainability of your Express application built with benhall/express-demo.