
Security News
High Salaries No Longer Enough to Attract Top Cybersecurity Talent
A survey of 500 cybersecurity pros reveals high pay isn't enough—lack of growth and flexibility is driving attrition and risking organizational security.
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,698 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.
Security News
A survey of 500 cybersecurity pros reveals high pay isn't enough—lack of growth and flexibility is driving attrition and risking organizational security.
Product
Socket, the leader in open source security, is now available on Google Cloud Marketplace for simplified procurement and enhanced protection against supply chain attacks.
Security News
Corepack will be phased out from future Node.js releases following a TSC vote.