Add a script to your package.json to run the tests:
"scripts": {
"test": "jest"
}
# Setup Tests
Create Tests directory
Create a tests directory in your project in the project root, adjacent to your src directory.
Create a Test
Tests are TypeScript files that end in .test.ts.
Here's an example;
import { MaternityCalc } from '../src/maternityCalc';
describe('MaternityCalc', () => {
const edd = new Date('2024-06-30');
const maternityCalc = new MaternityCalc(edd);
test('should calculate LMP date correctly', () => {
const expectedLmpDate = new Date('2023-09-24');
expect(maternityCalc.lmpDate.toISOString().split('T')[0]).toBe(expectedLmpDate.toISOString().split('T')[0]);
});
test('should calculate correct dayOf', () => {
// You need to adjust the test depending on the current date.
// Assuming today is 2024-06-30 for this test:
jest.setSystemTime(new Date('2024-06-30'));
expect(maternityCalc.dayOf).toBe(280);
});
test('should calculate correct weekOf', () => {
// Assuming today is 2024-06-30 for this test:
jest.setSystemTime(new Date('2024-06-30'));
expect(maternityCalc.weekOf).toBe(41); // 280 days is 40 weeks, plus 1 for 1-based index
});
test('should create instance from LMP date correctly', () => {
const lmp = new Date('2023-09-24');
const instance = MaternityCalc.createFromLMP(lmp);
const expectedEdd = new Date('2024-06-30');
expect(instance._edd.toISOString().split('T')[0]).toBe(expectedEdd.toISOString().split('T')[0]);
});
});