SSE | Sygnal Site Engine
Community ForumSupport Us!Micro-Consulting
  • Sygnal Site Engine ( SSE )
  • The SSE Architecture
  • What's New
  • Feature Roadmap
    • Component Architecture
    • File Copy & Augmented Builds
    • SA5 Integration
  • Installation & Setup
    • Setup Github Repository
    • Setup Netlify
      • jsDelivr
    • Setup Webflow
    • Advanced Install Notes
      • Creating a Persistent Test Env
      • Add SSE to an Existing Repo
  • Usage Notes
    • Developing with SSE
    • Building & Deploying Code
    • Code Structure
    • Page Router
    • Components
      • Components Future Notes
    • Source Structure & Key Files
      • Utilities
      • Route Dispatcher
      • Infrastructure
      • Usage Notes
    • SCSS
    • Useful Library Additions
      • Adding Libraries
      • Luxon
      • Cookies
      • Core Libraries
      • Extending Capabilities
    • Best Practices
    • Unit Testing
      • Using Google Sheets as a Unit Test Data Source
      • Datetime & Timezone Tests
      • Best Practices
      • Page 1
  • Sygnal Devproxy
    • What is Devproxy?
    • Devproxy Setup
    • Cloudflare Setup
    • Webflow Site Configuration
    • Configurations
      • Controlling deployment flow
    • Future
  • Tech Stack
    • Source Code Repository
    • Visual Studio Code
    • Developer IDE
    • Dev Hosting
    • Code CDN
    • Devproxy
  • Further Development
    • Dev Team Notes
      • Engine Mode
    • Reference Project
    • Future
      • Component Development
      • Reactive State Management
      • Expand Script Loading
    • Devmode
    • CI/CD Discussions
      • Page 2
    • SA5
  • Tools
    • Webflow Designer Notation
  • SPECIAL ENHANCEMENTS
    • cookie.js
    • PostHog
Powered by GitBook
On this page
  • Install Jest
  • jest.config.js
  • tsconfig.json
  • package.json
  • # Setup Tests
  • Create Tests directory
  • Create a Test
  • Run Tests
  1. Usage Notes

Unit Testing

Install Jest

In VSCode from a terminal;

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

jest.config.js

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

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:

{
  "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.

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

Run Tests

npm test

PreviousBest PracticesNextUsing Google Sheets as a Unit Test Data Source

Last updated 10 months ago