Socket
Book a DemoInstallSign in
Socket

jest-mock-exports

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-mock-exports

Easy mocking of exported functions and objects in Jest

latest
Source
npmnpm
Version
0.1.2
Version published
Maintainers
1
Created
Source

Jest Mock Exports

Simplifies mocking exports for Jest.

import { libFn } from 'some-library';
import { doStuff, logicFn, someConfig } from './my/file';
import { whatAreWeTesting } from './file';
import { mockExportedFn, mockExportedObject, spyOnExportedFn } from 'jest-mock-exports';

// mocks with jest.fn() by default:
const mocked1 = mockExportedFn(libFn)

// provide implementation:
const mocked2 = mockExportedFn(doStuff, () => 'mock implementation')

// mocking objects:
mockExportedObject(someConfig, { new: 'value' })

// spying on functions without changing their implementation:
const spy = spyOnExportedFn(logicFn)

it('works', async () => {
    mocked2.mockResolvedValueOnce('return for this test')
    
    await whatAreWeTesting()
    
    expect(mocked1).toHaveBeenCalledTimes(1)
    expect(mocked2).toHaveBeenCalledWith('params')
    expect(spy).toHaveBeenCalledWith('params')
})

For convenience, you can group the mocks:

const mocks = {
    fn1: mockExportedFn(fn1),
    fn2: mockExportedFn(fn1, () => 'mock implementation'),
    obj: mockExportedObject(obj, { new: 'value' }),
}

it('works', async () => {
    mocks.fn1.mockResolvedValueOnce('return for this test')
    
    await whatAreWeTesting()
    
    expect(mocks.fn2).toHaveBeenCalledWith('params')
})

Installation

npm i -D jest-mock-exports

Add to jest.config.ts (or .js) in the root:

import type { Config } from "jest";

const config: Config = {
    runtime: 'jest-mock-exports/jest.runtime',
};

export default config;

Keywords

jest

FAQs

Package last updated on 31 Aug 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts