Registry / testing / ts-mock-imports

ts-mock-imports

JSON →
library1.3.19jsnpmunverified

ts-mock-imports provides an intuitive way to mock ES6 `import` dependencies for TypeScript classes during unit testing, without requiring explicit dependency injection. It is built on top of `sinon` for stubbing capabilities and leverages TypeScript's module resolution to intercept imported classes. The library is currently at version 1.3.19 and primarily sees minor patch releases, focusing on dependency updates and bug fixes rather than rapid feature additions, indicating a mature and stable codebase. A key differentiator is its direct manipulation of imported modules to replace original classes with type-safe stub versions, enabling seamless testing of code that directly instantiates its dependencies. It intercepts and replaces actual class constructors or functions exported via ES6 `import` statements with Sinon stubs at runtime, allowing fine-grained control over dependencies without modifying the source code under test. It requires both `sinon` (version >= 4.1.2) and `typescript` (version >= 2.6.1) as peer dependencies to function correctly.

npm install ts-mock-imports
INSTALL
IMPORT
SIG · TS-MOCK-IMPORTS
T
ts-mock-imports
testingjavascriptv1.3.19
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ImportMock
import { ImportMock } from 'ts-mock-imports';
const { ImportMock } = require('ts-mock-imports');
ImportMock is the main entry point for creating mocks and should be imported as a named export. While the library itself might be callable via CommonJS in some build environments, the core mocking mechanism primarily operates on ES6 `import`s.
MockManager
import type { MockManager } from 'ts-mock-imports';
import { MockManager } from 'ts-mock-imports';
MockManager is a type interface for the object returned by `ImportMock.mockClass`. It should be imported as a type, not a value, to avoid potential runtime issues or unnecessary bundle size.
InPlaceMockManager
import { InPlaceMockManager } from 'ts-mock-imports';
const { InPlaceMockManager } = require('ts-mock-imports');
InPlaceMockManager is an alternative manager for specific scenarios, particularly when encountering 'Cannot set property' errors due to module getters. It's used for mocking imports where standard `ImportMock.mockClass` fails.

Demonstrates mocking a TypeScript class imported via ES6 syntax, replacing its constructor and methods with stubs to prevent execution of original logic and control return values.

import { ImportMock } from 'ts-mock-imports'; import { Bar } from '../src/bar'; import * as fooModule from '../src/foo'; // Imagine 'Foo' class in '../src/foo.ts' has a constructor that throws an error // and 'Bar' in '../src/bar.ts' instantiates 'Foo'. describe('Bar', () => { let mockFooManager: ImportMock.MockManager<any>; beforeEach(() => { // Mock the 'Foo' class from 'fooModule' // This intercepts any 'new Foo()' call within modules that imported fooModule mockFooManager = ImportMock.mockClass(fooModule, 'Foo'); }); afterEach(() => { // Restore all mocks to their original implementations to ensure test isolation ImportMock.restore(); }); it('should create an instance of Bar without error when Foo is mocked', () => { // Now, new Bar() will use the mocked Foo, preventing the error const bar = new Bar(); expect(bar).toBeInstanceOf(Bar); }); it('should allow stubbing methods of the mocked Foo class', () => { // Configure a mock response for the 'getCount' method of Foo mockFooManager.mock('getCount', () => 42); // If Bar were to call getCount internally, it would now return 42 // For demonstration, let's assume Bar had a method that called Foo.getCount() // (This example focuses on the core mocking setup) // const bar = new Bar(); // const result = bar.getFooCount(); // Assuming such a method exists // expect(result).toBe(42); const mockFooInstance = mockFooManager.get;// For a real test, you'd test Bar's interaction with Foo }); });
Debug
Known issues
gotchaWhen mocking a module, both the source file and the test file must use the *exact same path* to import the target module. Inconsistent import paths (e.g., `'./src/foo'` in production vs. `'src/index'` in tests for the same module) will prevent the mock from being applied correctly.
fix
Standardize module import paths across your project, often by using `tsconfig.json` `paths` aliases, or ensure relative paths are identical between consumer and test files.
affects: >=1.0.0
breakingTypeScript versions 3.9 and later introduced changes that can cause a `TypeError: Cannot set property TestClass of #<Object> which has only a getter` when attempting to mock certain modules. This occurs because module exports are no longer enumerable, preventing `ts-mock-imports` from replacing the exported class.
fix
For modules exhibiting this error, use `InPlaceMockManager` instead of `ImportMock.mockClass`. This manager is designed to work around the getter issue. Example: `const mockManager = new InPlaceMockManager(fooModule, 'Foo');`
affects: >=1.3.0
gotchaIt is critical to call `ImportMock.restore()` after each test or test suite where mocks were applied. Failure to restore mocks can lead to global state pollution, causing tests to interfere with each other and produce flaky or incorrect results.
fix
Always include `ImportMock.restore()` in an `afterEach` or `afterAll` hook in your test setup to reset the module state.
affects: >=1.0.0
gotchaWhile `ts-mock-imports` allows mocking ES6 `import`s, it relies on runtime manipulation of module exports. This mechanism is fundamentally a 'monkey patch' over Node's module system and may be less robust or compatible with native ESM environments where imports are often immutable.
fix
Be aware that complex module bundlers (e.g., Webpack, Rollup) or newer Node.js ESM loading behaviors might introduce challenges. Consider alternative dependency injection patterns if encountering consistent issues, or ensure your testing environment aligns with the library's assumptions about module loading.
affects: >=1.0.0
gotcha`ts-mock-imports` depends on `sinon` and `typescript` as peer dependencies. These must be installed manually in your project (typically as `devDependencies`) for the library to function correctly.
fix
Ensure `npm install sinon typescript --save-dev` has been run in your project.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot set property TestClass of #<Object> which has only a getter
Attempting to mock a class from a module that defines its exports with getters, preventing modification. This is common with certain TypeScript compilation targets or bundler configurations (e.g., Babel 7+, Webpack).
fix
Replace `ImportMock.mockClass` with `new InPlaceMockManager(module, 'ClassName');` to use an alternative mocking strategy that bypasses the getter restriction.
ReferenceError: [ClassName] is not defined
The module path provided to `ImportMock.mockClass` in your test file does not precisely match the path used by the module under test that imports `ClassName`.
fix
Verify that `import * as myModule from './path/to/module';` in your test file uses the exact same relative or aliased path as the file that imports `ClassName` in your application code.
Cannot find module 'sinon' or Cannot find module 'typescript'
The peer dependencies `sinon` and/or `typescript` are not installed in the project.
fix
Install the required peer dependencies: `npm install sinon typescript --save-dev`.
TypeError: module.exports is not a function or module.exports.default is not a constructor
`ts-mock-imports` is designed for mocking ES6 `import`s of classes. This error can occur if you're trying to mock a CommonJS `require`'d module, or if the export structure doesn't match an ES6 class export.
fix
Ensure the module you are trying to mock is indeed exporting a class via ES6 `export class MyClass { ... }` or `export default class MyClass { ... }` and is being consumed via `import` statements. The library is not compatible with `requirejs`.
Upgrade
Version history
1.3.19latest on npm
Audit
Dependencies
sinonrequiredCore mocking and stubbing functionality.
typescriptrequiredRequired for type safety and compilation.
Agent activity
4 hits · last 30 days
node
4
Resources
ts-mock-imports — npm install ts-mock-imports · libregistry