diff --git a/packages/codemods/src/__tests__/method-codemods.test.js b/packages/codemods/src/__tests__/method-codemods.test.js index 9d099c76..54e25de7 100644 --- a/packages/codemods/src/__tests__/method-codemods.test.js +++ b/packages/codemods/src/__tests__/method-codemods.test.js @@ -66,35 +66,21 @@ describe('codemods operating on methods', () => { describe('converting resetting methods', () => { it('rewrites restore()', () => { - expectCodemodResult( - 'fetchMock.restore()', - `fetchMock.clearHistory(); -fetchMock.removeRoutes(); -fetchMock.unmockGlobal();`, - ); + expectCodemodResult('fetchMock.restore()', `fetchMock.hardReset();`); }); it('rewrites restore() with {sticky: true}', () => { expectCodemodResult( 'fetchMock.restore({sticky: true})', - `fetchMock.clearHistory(); -fetchMock.removeRoutes({includeSticky: true}); -fetchMock.unmockGlobal();`, + `fetchMock.hardReset({includeSticky: true});`, ); }); it('rewrites reset()', () => { - expectCodemodResult( - 'fetchMock.reset()', - `fetchMock.clearHistory(); -fetchMock.removeRoutes(); -fetchMock.unmockGlobal();`, - ); + expectCodemodResult('fetchMock.reset()', `fetchMock.hardReset();`); }); it('rewrites reset() with {sticky: true}', () => { expectCodemodResult( 'fetchMock.reset({sticky: true})', - `fetchMock.clearHistory(); -fetchMock.removeRoutes({includeSticky: true}); -fetchMock.unmockGlobal();`, + `fetchMock.hardReset({includeSticky: true});`, ); }); diff --git a/packages/codemods/src/codemods/methods.js b/packages/codemods/src/codemods/methods.js index 5ed3cf2d..ec5c6a43 100644 --- a/packages/codemods/src/codemods/methods.js +++ b/packages/codemods/src/codemods/methods.js @@ -74,11 +74,16 @@ module.exports.simpleMethods = function (fetchMockVariableName, root) { const sticky = path?.value?.arguments[0]?.properties?.find( (prop) => prop.key.name === 'sticky', )?.value?.value; - const newExpressions = j(` -${methodName !== 'resetBehavior' ? 'fetchMock.clearHistory()' : ''}; + let expressionsString; + if (methodName === 'resetBehavior') { + expressionsString = ` fetchMock.removeRoutes(${sticky ? '{includeSticky: true}' : ''}); -fetchMock.unmockGlobal(); -`) +fetchMock.unmockGlobal();`; + } else { + expressionsString = ` +fetchMock.hardReset(${sticky ? '{includeSticky: true}' : ''});`; + } + const newExpressions = j(expressionsString) .find(j.ExpressionStatement) .paths(); const insertLocation = j(path) diff --git a/packages/fetch-mock/src/FetchMock.ts b/packages/fetch-mock/src/FetchMock.ts index 9f6a0549..aad608c0 100644 --- a/packages/fetch-mock/src/FetchMock.ts +++ b/packages/fetch-mock/src/FetchMock.ts @@ -4,9 +4,9 @@ import { MatcherDefinition, RouteMatcher } from './Matchers.js'; import CallHistory from './CallHistory.js'; import * as requestUtils from './RequestUtils.js'; -export type HardResetOptions = { +export type HardResetOptions = { includeSticky?: boolean; -} +}; export type FetchMockGlobalConfig = { includeContentLength?: boolean;