Hone logo
Hone
Problems

Mastering TypeScript Testing with ts-jest

This challenge focuses on setting up and utilizing ts-jest with Jest, a popular JavaScript testing framework. The goal is to enable Jest to understand and execute TypeScript code directly, eliminating the need for manual transpilation steps before running tests. This is a fundamental skill for any TypeScript developer looking to build robust and maintainable applications.

Problem Description

Your task is to configure a project to use ts-jest for running Jest tests on TypeScript code. You will need to set up a basic project structure, install the necessary dependencies, configure Jest to use ts-jest, and then write a simple TypeScript function and its corresponding Jest test to demonstrate that ts-jest is working correctly.

What needs to be achieved:

  1. Set up a new Node.js project.
  2. Install Jest and ts-jest as development dependencies.
  3. Configure Jest to use ts-jest as its preprocessor.
  4. Create a simple TypeScript function.
  5. Write a Jest test for that function using TypeScript.
  6. Successfully run the tests using Jest.

Key requirements:

  • The project should be initialized with npm init -y or yarn init -y.
  • jest and ts-jest must be installed as dev dependencies (npm install --save-dev jest ts-jest or yarn add --dev jest ts-jest).
  • A jest.config.js file (or equivalent configuration within package.json) must be created to specify ts-jest as the transformer.
  • The TypeScript code and tests should be written in .ts files.

Expected behavior: When you run the Jest command (e.g., npm test or yarn test), the test for your TypeScript function should pass without any errors related to TypeScript syntax or compilation.

Important edge cases to consider:

  • Ensure the Jest configuration correctly points to ts-jest.
  • Verify that all dependencies are installed correctly.

Examples

Example 1: Basic Sum Function

Project Setup:

  1. Initialize project: npm init -y
  2. Install dependencies: npm install --save-dev jest ts-jest @types/jest
  3. Create jest.config.js:
    module.exports = {
      preset: 'ts-jest',
      testEnvironment: 'node',
    };
    
  4. Create src/math.ts:
    export function add(a: number, b: number): number {
      return a + b;
    }
    
  5. Create src/__tests__/math.test.ts:
    import { add } from '../math';
    
    describe('add function', () => {
      test('should return the sum of two numbers', () => {
        expect(add(5, 3)).toBe(8);
      });
    });
    
  6. Add a test script to package.json:
    "scripts": {
      "test": "jest"
    }
    

Command to run:

npm test

Expected Output (simplified):

PASS  src/__tests__/math.test.ts
  add function
    ✓ should return the sum of two numbers (XXms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        X.XXXs, estimated X more tasks

Explanation: The jest.config.js file tells Jest to use the ts-jest preset, which automatically configures ts-jest to transform TypeScript files before Jest runs them. The test successfully imports and executes the TypeScript add function, verifying its correctness.

Constraints

  • The project must be a standard Node.js project.
  • Dependencies must be installed using either npm or yarn.
  • The Jest configuration must be in a dedicated file (jest.config.js, jest.config.ts, or within package.json).
  • The test execution time is not a primary concern for this challenge, but it should be reasonably fast for a simple function.

Notes

  • Remember to install @types/jest as well, as Jest's typings are crucial for writing TypeScript tests.
  • You can explore different Jest configuration options in the jest.config.js file, but for this challenge, setting the preset to 'ts-jest' is the most important step.
  • Consider what happens if you forget to install dependencies or misconfigure the Jest settings. How would you debug those issues?
Loading editor...
typescript