JSPM

jests-mock-utils

1.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q16137F
  • License MIT

A TypeScript-Jest utility library that simplifies the creation of strongly-typed mocks.

Package Exports

  • jests-mock-utils
  • jests-mock-utils/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (jests-mock-utils) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

JesTS Mock Utils

A TypeScript-Jest utility library that simplifies the creation of strongly-typed mocks.

Background

The type-safety of Typescript is great - it gives us confidence that the code we write is doing the right thing, and helps us catch bugs nice and early in the development process. But this type-safety can feel overly restrictive when writing tests. The boilerplate required to create mocks that satisfy both functional and mock-specific type constraints is cumbersome and makes the test-writing process frustrating, as well as leading to clutter in our test files.

This package aims to alleviate these frustrations, to allow us to make the most of Typescript's type-safety in a testing context, without slowing us down!

Features

  • Strongly Typed Mocks: Get full IntelliSense support and type checking.
  • Convenient Mock Creation: Easily create mock objects, classes, and functions with strong types.
  • Partial and Deep Partial Mocks: Return partial objects from your mock functions, even deeply nested ones.

Installation

npm install jests-mock-utils

Example Usage

The following example shows how to create a mock object from a type.

// databaseClient.ts
export interface DatabaseClient {
    query(query: string): DatabaseResponse;
}

// dataFetcher.ts
export class DataFetcher {
    private client: DatabaseClient;

    constructor(client: DatabaseClient) {
        this.client = client;
    }

    fetchAllCustomers(): Customer[] {
        return this.extractCustomersFromResponse(client.query());
    }
}

// dataFetcher.ts
import { DatabaseClient } from '../databaseClient';
import { DataFetcher } from '../dataFetcher';
import { createMockFromType } from 'jests-mock-utils';

const mockDatabaseClient = createMockFromType<DatabaseClient>();
const testDataFetcher = new DataFetcher(mockDatabaseClient);

API Documentation