Security News
New Python Packaging Proposal Aims to Solve Phantom Dependency Problem with SBOMs
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.
@fetch-mock/codemods
Advanced tools
A tool for helping upgrade to fetch-mock@12.
npm i -D @fetch-mock/codemods jscodeshift fetch-mock@12
;.sandbox()
according to the example at the bottom of this page.jscodeshift -t node_modules/@fetch-mock/codemods/src/index.js --ignore-pattern="node_modules/**/*" .
to run over your entire project, or replace .
with the directory/file paths you wish to modify. The jscodeshift CLI has many options - adjust them to suit your project. Note that the parser option should not be used as @fetch-mock/codemods forces use of the TSX parser in order to ensure compatibility with teh greatest range of projects.jest.mock()
or fetch-mock-jest) you may pass in one or more variable names using the FM_VARIABLES
environment variable e.g. FM_VARIABLES=fm,fetch
fetch
you will probably need to add a call to .mockGlobal()
at least once per test suite, possibly in a beforeAll
/beforeEach
block.
b) Correct all the errors inserted by the codemod related to use of fallbackToNetwork
, .calls()
or .lastCall()
usage.
c) Run all your tests and fix any issues. If you believe the codemods have made any errors, or have missed any easy to modify patterns, please raise an issue.npm uninstall @fetch-mock/codemods jscodeshift
If you're using fetch-mock-jest, you should migrate to @fetch-mock/jest, which is built around fetch-mock@12. While these codemods have not been tested on fetch-mock-jest, in principle you should be able use them by making similar adjustments to those described below about use with the sandbox()
method, and by using the FM_VARIABLES
environment variable to help the codemod identify which variables contain instances of fetch-mock.
For everyt variable containing an instance of fetch-mock imported using require
or import
it will:
.mock()
to .route()
.reset()
, .restore()
, .resetBehavior()
and .resetHistory()
to their equivalents in fetch-mock@12.lastUrl()
, .lastOptions()
and lastResponse()
to their equivalents in fetch-mock@12.lastCall()
or .calls()
are used with advice on how to manually correct these.getOnce()
, .getAnyOnce()
, .postOnce()
etc... - which have been removed - to calls to the underlying .get()
method with additional options passed in.overwriteRoutes
, warnOnFallback
, sendAsJson
fallbackToNetwork
option, and adds an informative error with details of how to replace with the spyGlobal()
method.mock()
method which combines defining a route and setting up global mocking. All calls to .mock()
are replaced by .route()
.
If using global fetch
you will also need to call .mockGlobal()
at least once per test suite..sandbox()
method previously created a fetch
function that also had all the fetch-mock methods attached. This is no longer the case, but pulling it apart is complex and deliberately left out of scope for this codemod.require('fetch-mock').mock('a', 'b')
will not be converted to require('fetch-mock').route('a', 'b')
jest.mock('node-fetch', () => require('fetch-mock').sandbox())
, the codemod is unable to identify that require('node-fetch')
will be an instance of fetch-mock.fetch
. In most cases, even if your application code still uses node-fetch, your mocks will still work. However, if you explicitly create instances of Request
or Headers
using node-fetch's classes, you may need to provide these to fetch-mock.Taking the last 4 points together, this example illustrates the kind of manual modifications required:
jest.mock('node-fetch', () => require('fetch-mock').sandbox());
const nodeFetch = require('node-fetch');
it('runs a test', () => {
nodeFetch.get('http://a.com', 200);
myAPI.call();
expect(nodeFetch.called()).toBe(true);
});
const fetchMock = require('fetch-mock');
jest.mock('node-fetch', () => {
const nodeFetch = jest.requireActual('node-fetch');
// only needed if your application makes use of Response, Request
// or Headers classes directly
Object.assign(fetchMock.config, {
fetch: nodeFetch,
Response: nodeFetch.Response,
Request: nodeFetch.Request,
Headers: nodeFetch.Headers,
});
return fetchMock.fetchHandler;
});
const nodeFetch = require('node-fetch');
it('runs a test', () => {
fetchMock.get('http://a.com', 200);
myAPI.call();
expect(fetchMock.called()).toBe(true);
});
FAQs
Codemods for upgrading fetch-mock
We found that @fetch-mock/codemods demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 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
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.
Security News
Socket CEO Feross Aboukhadijeh discusses open source security challenges, including zero-day attacks and supply chain risks, on the Cyber Security Council podcast.
Security News
Research
Socket researchers uncover how threat actors weaponize Out-of-Band Application Security Testing (OAST) techniques across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.