Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
chai-fetch-mock
Advanced tools
Chai plugin for fetch-mock library
A set of Chai assertions for fetch-mock, a mocking library for fetch()
and isomorphic-fetch.
This library depends on:
"peerDependencies": {
"chai": "3.x || 4.x",
"fetch-mock": "5.1.x || 6.x"
}
Note: fetch-mock 6.0 is only compatible with Node.js 7 or greater.
Install those, and then install:
npm install chai-fetch-mock
Note: if you want to use this plugin with other Chai plugins that use a similar vocabulary—such as sinon-chai, which also has a called
assertion—apply chai-fetch-mock last. chai-fetch-mock's methods will only kick in when an assertion calls the route()
method.
import chai from 'chai';
import chaiFetchMock from 'chai-fetch-mock';
import fetchMock from 'fetch-mock';
// Call conflicting plugins before
// chai.use(sinonChai)
chai.use(chaiFetchMock);
describe('test', () => {
before(() => fetchMock.get('/cats', { cats: 5 }))
it('calls fetch', () => {
return fetch('/cats').then(() => {
expect(fetchMock).route('/cats').to.have.been.called;
});
});
after(() => fetchMock.restore());
});
Sets up an assertion to check calls to a matcher with the name value
. This is either the URL of the route, or the custom name of the route. Use this before any other fetch-mock assertion.
// Default name
fetchMock.get('*', {});
expect(fetchMock).route('*');
// Custom name
fetchMock.get(/.*/, {}, { name: 'all' });
expect(fetchMock).route('all');
This function on its own only asserts that the mock route exists.
Asserts that fetch()
was used to call a specific route at least once.
expect(fetchMock).route('*').to.have.been.called;
expect(fetchMock).route('*').to.not.have.been.called;
This method can be chained to the ones below, allowing you to check if a route was called and if it was called with specific properties in one assertion.
Asserts that the arguments of last call to fetch()
to a specific route deeply equal value
.
getCat(1).then(() => {
expect(fetchMock).route('/cats/get').to.have.been.called.with.args(['/cats/get', { id: 1 }]);
});
Asserts that the URL of last call to fetch()
to a specific route equals value
.
getDoggo(2).then(() => {
expect(fetchMock).route('/doggos/get').to.have.been.called.with.url('/doggos/get/2');
});
Asserts that the options of last call to fetch()
to a specific route deeply equal value
.
getDoggo(2).then(() => {
expect(fetchMock).route('/doggos/get').to.have.been.called.with.options({ mode: 'same-origin' });
});
MIT © Geoff Kimball
FAQs
Chai plugin for fetch-mock library
The npm package chai-fetch-mock receives a total of 909 weekly downloads. As such, chai-fetch-mock popularity was classified as not popular.
We found that chai-fetch-mock 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
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.