
Product
Introducing Tier 1 Reachability: Precision CVE Triage for Enterprise Teams
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
jest-helpers
Advanced tools
TypeScript helper functions for Jest to help make your tests resilient to refactoring.
Tests should be living documentation for your code, but often test descriptions get out of sync with your code. This library helps keep them in sync using TypeScript.
partialOf<T>(partial: Partial<T>): T
deepPartialOf<T>(partial: DeepPartial<T>): T
npm install jest-helpers --save-dev
./greeter.ts
export class Greeter {
getGreeting(name: string) {
return `Hello ${name}`;
}
}
export function showGreeting(greeter: Greeter, name: string) {
const greeting = greeter.getGreeting(name)
console.log(greeting)
}
./greeter.test.ts
import { describeClass, describeFunction, describeMethod, partialOf } from 'jest-helpers';
import greeterModule = require('greeter');
import { Greeter, showGreeting } from './greeter';
describeClass(Greeter, () => {
describeMethod(Greeter, 'getGreeting', () => {
it('should return a personalised greeting', () => {
const greeter = new Greeter();
expect(greeter.getGreeting('Joe')).toEqual('Hello Joe');
});
});
});
describeFunction(greeterModule, 'showGreeting', () => {
it('should log greeting to the console', () => {
const greeterMock = partialOf<Greeter>({
getGreeting: jest.fn().mockReturnValue('yo!')
});
jest.spyOn(console, 'log');
showGreeting(greeterMock, 'Joe');
expect(greeterMock.getGreeting).toHaveBeenCalledWith('Joe');
expect(console.log).toHaveBeenCalledWith('yo!');
});
});
Running jest --verbose
will output something like
example/greeter.ts
Greeter
getGreeting
✓ should return a personalised greeting (4ms)
showGreeting
✓ should log greeting to the console (2ms)
If you rename your Greeter
class, it will automatically update the test description.
If you rename your Greeter.getGreeting
method, you will get a TypeScript error in your test until you update your test to match the new name.
If you rename your showGreeting
function, you will get a TypeScript error in your test until you update your test to match the new name.
Got an issue or a feature request? Log it.
Pull-requests are also welcome. 😸
FAQs
TypeScript helper functions for Jest to help make your tests resilient to refactoring.
We found that jest-helpers demonstrated a not healthy version release cadence and project activity because the last version was released 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.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.
Security News
The MCP Steering Committee has launched the official MCP Registry in preview, a central hub for discovering and publishing MCP servers.