Lesson 28 of 55 · javascript
Testing JavaScript – Unit & Integration
Duration: 12 mins
Popular test frameworks:
- Jest – zero‑config, snapshot testing, mocking.
- Mocha + Chai – flexible BDD/TDD style.
- Cypress – end‑to‑end browser testing.
Example: Jest unit test
// sum.js
export const sum = (a,b) => a + b;
// sum.test.js
import {sum} from './sum.js';
test('adds two numbers', () => {
expect(sum(1,2)).toBe(3);
});
Run with npm test.
Example: Cypress basic test
describe('My App',()=>{
it('shows hello message',()=>{
cy.visit('/');
cy.contains('h1','Hello');
});
});