Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
@async-fn/jest
Advanced tools
Additional methods to jest.fn to introduce "late resolve" of promises returned by mock functions. This allows tests that read chronologically, like a story.
asyncFn for jest provides additional methods to jest.fn to introduce "late resolve" for the promises returned.
This simplifies async unit testing by allowing tests that read chronologically, like a story, and do not require excessive test setup to know beforehand how async mocks are supposed to behave in each scenario.
It has 100% unit test coverage, and 3+ years of focused production use with high developer satisfaction.
asyncFn is also integration-tested for combinations of recent OS-, node- and mocking framework -versions.
$ npm install --save-dev @async-fn/jest
See tutorial here.
import asyncFn from '@async-fn/jest';
it('given called, a result can be resolved *after* the mock is called', async () => {
const mockFunction = asyncFn();
const promise = mockFunction();
// Note how we resolve the returned promise *after* it is called.
// This permits us to write our tests so that they read like a story.
await mockFunction.resolve('some-value');
const actual = await promise;
expect(actual).toBe('some-value');
});
import asyncFn from '@async-fn/jest';
it('given called multiple times, the results can be resolved *after* the mock was called', async () => {
const mockFunction = asyncFn();
const promise = Promise.all([mockFunction(), mockFunction(), mockFunction()]);
await mockFunction.resolve('some-first-value');
await mockFunction.resolve('some-second-value');
await mockFunction.resolve('some-third-value');
const actual = await promise;
expect(actual).toEqual([
'some-first-value',
'some-second-value',
'some-third-value',
]);
});
import asyncFn from '@async-fn/jest';
it('can be awaited to test the coincidences of resolve', async () => {
const mockFunction = asyncFn();
let coincidenceHasHappened = false;
mockFunction()
.then()
.then()
.then(() => {
coincidenceHasHappened = true;
});
await mockFunction.resolve();
expect(coincidenceHasHappened).toBe(true);
});
import asyncFn from '@async-fn/jest';
it('can be rejected with a rejection', () => {
const mockFunction = asyncFn();
const promise = mockFunction();
mockFunction.reject('some-rejection');
return expect(promise).rejects.toBe('some-rejection');
});
import asyncFn from '@async-fn/jest';
it('does what jest.fn does', () => {
const mockFunction = asyncFn();
mockFunction('some-argument', 'some-other-argument');
expect(mockFunction).toHaveBeenCalledWith(
'some-argument',
'some-other-argument',
);
});
Check out the unit tests.
Currently asyncFn is also available for sinon.
asyncFn is lovingly crafted by Your pals at Team: Igniter from Houston Inc. Consulting.
We are a software development team of friends, with proven tradition in professional excellence. We specialize in holistic rapid deployments without sacrificing quality.
Come say hi at Gitter, email us, or check out the team's website. We just might be open to hiring ;)
FAQs
Additional methods to jest.fn to introduce "late resolve" of promises returned by mock functions. This allows tests that read chronologically, like a story.
The npm package @async-fn/jest receives a total of 602 weekly downloads. As such, @async-fn/jest popularity was classified as not popular.
We found that @async-fn/jest demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 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
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.