> For the complete documentation index, see [llms.txt](https://engine.sygnal.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://engine.sygnal.com/usage/unit-testing.md).

# Unit Testing

## Install Jest

In VSCode from a terminal;&#x20;

```
npm install --save-dev jest ts-jest @types/jest
```

## jest.config.js

Create a `jest.config.js` file in your project root:

```javascript
module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
};
```

## tsconfig.json

**Update `tsconfig.json`:**

Ensure your `tsconfig.json` is set up to handle Jest and TypeScript. Add the following configuration if it's not already present:

```json
{
  "compilerOptions": {
    "target": "ES6",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "types": ["jest"]
  }
}
```

## package.json

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`. &#x20;

Here's an example;

```typescript
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]);
  });
});

```

## Run Tests

```
npm test
```
