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:
- 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
- Create a
test
directory and amocha.json
file inside it:
mkdir test
touch test/mocha.json
- 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"
]
}
- Create test files for routes:
Create two test files, index.test.js
and users.test.js
, inside the test
directory.
- 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));
});
});
- 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));
});
});
- 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');
});
});
- Run the tests:
In your terminal, run the following command to execute all tests:
npm test
Tests:
- Unit tests for the
index.js
route. - Integration tests for the
users.js
route. - 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.