Testing is crucial for ensuring your application works correctly.
a. Install Testing Libraries
For unit and integration tests, you might use libraries like Mocha and Chai.
npm install mocha chai supertest
b. Write Tests
Create a test
directory and a test file, e.g., test/app.test.js
:
const request = require('supertest');
const app = require('../app'); // Your Express app
describe('GET /', () => {
it('should return Hello World', async () => {
const response = await request(app).get('/');
expect(response.text).toBe('Hello World!');
});
});
c. Run Tests
Add a test script in package.json
:
"scripts": {
"test": "mocha"
}
Run your tests:
bashCopy codenpm test
Leave a Reply