Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
fetch-mock-jest
Advanced tools
Readme
Wrapper around fetch-mock - a comprehensive, isomorphic mock for the fetch api - which provides an interface that is more idiomatic when working in jest.
The example at the bottom of this readme demonstrates the intuitive API, but shows off only a fraction of fetch-mock's functionality. Features include:
fetch-mock-jest requires the following to run:
node-fetch
is not included as a dependency of fetch-mock
.fetch
API either natively or via a polyfill/ponyfillnpm install -D fetch-mock-jest
const fetchMock = require('fetch-mock-jest')
jest.mock('node-fetch', () => require('fetch-mock-jest').sandbox())
const fetchMock = require('node-fetch')
Please refer to the fetch-mock documentation and cheatsheet
All jest methods for configuring mock functions are disabled as fetch-mock's own methods should always be used
All the built in jest function inspection assertions can be used, e.g. expect(fetchMock).toHaveBeenCalledWith('http://example.com')
.
fetchMock.mock.calls
and fetchMock.mock.results
are also exposed, giving access to manually inspect the calls.
The following custom jest expectation methods, proxying through to fetch-mock
's inspection methods are also available. They can all be prefixed with the .not
helper for negative assertions.
expect(fetchMock).toHaveFetched(filter, options)
expect(fetchMock).toHaveLastFetched(filter, options)
expect(fetchMock).toHaveNthFetched(n, filter, options)
expect(fetchMock).toHaveFetchedTimes(n, filter, options)
expect(fetchMock).toBeDone(filter)
filter
and options
are the same as those used by fetch-mock
's inspection methodsFetched
replaced by any of the following verbs to scope to a particular method: + Got + Posted + Put + Deleted + FetchedHead + Patchede.g. expect(fetchMock).toHaveLastPatched(filter, options)
fetchMock.mockClear()
can be used to reset the call history
fetchMock.mockReset()
can be used to remove all configured mocks
Please report any bugs in resetting mocks on the issues board
const fetchMock = require('fetch-mock-jest');
const userManager = require('../src/user-manager');
test(async () => {
const users = [{ name: 'bob' }];
fetchMock
.get('http://example.com/users', users)
.post('http://example.com/user', (url, options) => {
if (typeof options.body.name === 'string') {
users.push(options.body);
return 202;
}
return 400;
})
.patch(
{
url: 'http://example.com/user'
},
405
);
expect(await userManager.getAll()).toEqual([{ name: 'bob' }]);
expect(fetchMock).toHaveLastFetched('http://example.com/users
get');
await userManager.create({ name: true });
expect(fetchMock).toHaveLastFetched(
{
url: 'http://example.com/user',
body: { name: true }
},
'post'
);
expect(await userManager.getAll()).toEqual([{ name: 'bob' }]);
fetchMock.mockClear();
await userManager.create({ name: 'sarah' });
expect(fetchMock).toHaveLastFetched(
{
url: 'http://example.com/user',
body: { name: 'sarah' }
},
'post'
);
expect(await userManager.getAll()).toEqual([
{ name: 'bob' },
{ name: 'sarah' }
]);
fetchMock.mockReset();
});
FAQs
Jest wrapper for fetch-mock, a comprehensive stub for fetch
The npm package fetch-mock-jest receives a total of 75,561 weekly downloads. As such, fetch-mock-jest popularity was classified as popular.
We found that fetch-mock-jest 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.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.