
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
jest-mock-exports
Advanced tools
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')
})
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;
FAQs
Easy mocking of exported functions and objects in Jest
We found that jest-mock-exports demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.