# 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
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://engine.sygnal.com/usage/unit-testing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
