Skip to content

atlassian-labs/unstoppable-mockery

@atlassian/unstoppable-mockery

Atlassian license npm version PRs Welcome

Unstoppable Mockery is a lightweight utility for creating type-safe, fully mocked classes and interfaces for Jest tests. Unlike jest.mock('<module-path>'), it allows you to mock classes and interfaces directly, with full TypeScript support and without relying on module boundaries. This makes your tests more maintainable, readable, and less brittle to refactoring.

Usage

Mocking Classes

import { mockClass } from '@atlassian/unstoppable-mockery';

class MyService {
  doSomething() { /* ... */ }
  getValue() { return 42; }
}

const mock = mockClass(MyService, { getValue: () => 100 });

// All prototype methods are jest.fn()
expect(jest.isMockFunction(mock.doSomething)).toBe(true);

// Overridden property
expect(mock.getValue()).toBe(100);

Mocking Interfaces

import { mockInterface } from '@atlassian/unstoppable-mockery';

interface ApiClient {
  fetchData: (id: string) => Promise<any>;
  isConnected: boolean;
}

const mockApi = mockInterface<ApiClient>({ isConnected: true });

// Dynamically created jest.fn() for interface methods
mockApi.fetchData.mockResolvedValue({ data: 'test' });

// Override property is used
expect(mockApi.isConnected).toBe(true);

// Any property access creates a mock function
expect(jest.isMockFunction(mockApi.fetchData)).toBe(true);

Why use @atlassian/unstoppable-mockery instead of jest.mock?

  • Type Safety: Directly mock classes and interfaces with full TypeScript support.
  • No Module Boundaries: Mock any class/interface, even if not exported from a module.
  • Refactor-Friendly: Your tests won't break if you move or rename files/classes.
  • Fine-Grained Control: Override specific methods/properties easily.
  • Cleaner Tests: No need for manual mock implementations or resetting modules.

Installation

npm install @atlassian/unstoppable-mockery --save-dev

Documentation

See the source code and TSDoc comments for API details. For advanced usage, refer to the examples in the repository.

Tests

To run tests:

npm test

Releasing

Releases are published to Artifactory npm-public and forwarded to npmjs.org using Changesets.

How to release

  1. In your PR branch, run yarn changeset and follow the prompts to describe your changes and select a version bump type (patch/minor/major). This creates a changeset file — commit it with your PR.
  2. Merge your PR to main.
  3. The Release workflow will automatically open a "Version Packages" PR that bumps the version and updates the CHANGELOG.
  4. Review and merge the "Version Packages" PR — the workflow will then publish the new version to Artifactory automatically.

Note: Publishing uses atlassian-labs/artifact-publish-token for authentication — no manual npm credentials are required.

Contributions

Contributions to @atlassian/unstoppable-mockery are welcome! Please see CONTRIBUTING.md for details.

License

Copyright (c) 2025 Atlassian US., Inc. Apache 2.0 licensed, see LICENSE file.

With ❤️ from Atlassian

About

Unstoppable Mockery is a lightweight utility for creating type-safe, fully mocked classes and interfaces for Jest tests. Unlike jest.mock('<module-path>'), it allows you to mock classes and interfaces directly, with full TypeScript support and without relying on module boundaries.

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Contributors