
Product
Introducing Webhook Events for Pull Request Scans
Add real-time Socket webhook events to your workflows to automatically receive pull request scan results and security alerts in real time.
jest-mock-proxy
Advanced tools
Mock classes and objects with the power of proxies!
Creates a Proxy
that will dynamically create spies when a property is accessed the first time.
Every subsequent access will use the same spy. In combination with TypeScript this allows us to create a mock for any class/object without having to specify all its properties and methods.
tl;dr;
Proxy
makes any property and method available on the mock at runtime.Requires node 8+.
$ yarn add -D jest-mock-proxy
or
$ npm install -D jest-mock-proxy
// service.ts
export class Service {
foo() {
console.log('hello');
}
bar(s: string) {
return s;
}
}
// some.test.ts
import { createMockProxy } from 'jest-mock-proxy';
import { service } from './service';
const mock = createMockProxy<typeof Service>();
mock.foo();
mock.bar.mockReturnValue('some string');
mock.bar('test'); // 'some string'
import { Client } from 'elasticsearch';
import { createMockProxy } from 'jest-mock-proxy';
import fixture from './__fixtures__/elastic-response.json';
// This is an imaginary service that depends on the elastic search client.
import createService from './createService';
const client = createMockProxy<Client>();
const service = createService(client);
beforeEach(() => {
client.mockClear();
client.search.mockResolvedValue(fixture);
});
test('use service to query', async () => {
await service.query('https://example.com?q=hello');
expect(client.search.mock.calls).toMatchSnapshot();
});
When you need to mock a dependency via jest.mock
, because you have no access to the module.
// query.ts
import { Pool, PoolConfig } from 'pg';
// 😨 This makes testing hard...
const pool = new Pool();
export const query = async (q: string, values?: any[]) => {
// ...because how to mock this?
const { rows } = pool.query(q, values);
return rows;
};
// query.test.ts
import { Pool } from 'pg';
import { createProxyFromMock } from 'jest-mock-proxy';
import { query } from './query';
jest.mock('pg');
const mockedPool = createProxyFromMock(Pool);
test('you can now mock the pool.query', async () => {
// Use mockedPool so you get good type inference from TS
mockedPool.query.mockResolvedValue({ rows: [{ id: 1, data: 'data' }] });
await query('SELECT * FROM table1'); // returns `[{ id: 1, data: 'data' }]`
});
3.1.2 (2022-05-19)
[object Object]## 3.1.1 (2022-05-19)
[object Object]# 3.0.0 (2019-03-22)
createProxyFromMock
(#2) (cfdd310), closes [#2](https://github.com/sebald/jest-mock-proxy/issues/✨ Introduce createProxyFromMock
(#2/[object Object]
FAQs
Mock classes and objects with the power of proxies!
The npm package jest-mock-proxy receives a total of 3,950 weekly downloads. As such, jest-mock-proxy popularity was classified as popular.
We found that jest-mock-proxy demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers 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.
Product
Add real-time Socket webhook events to your workflows to automatically receive pull request scan results and security alerts in real time.
Research
The Socket Threat Research Team uncovered malicious NuGet packages typosquatting the popular Nethereum project to steal wallet keys.
Product
A single platform for static analysis, secrets detection, container scanning, and CVE checks—built on trusted open source tools, ready to run out of the box.