diff --git a/packages/codemods/src/__test__/method-codemods.test.js b/packages/codemods/src/__test__/method-codemods.test.js index 63da6be6..b16ad0f0 100644 --- a/packages/codemods/src/__test__/method-codemods.test.js +++ b/packages/codemods/src/__test__/method-codemods.test.js @@ -2,18 +2,22 @@ import { describe, it, expect } from 'vitest'; import { codemod } from '../index'; import jscodeshift from 'jscodeshift'; -const prependFetchMock = (src) => - `const fetchMock = require('fetch-mock');\n${src}`; +const prependFetchMock = (src, fetchMockVariableName) => + `const ${fetchMockVariableName} = require('fetch-mock');\n${src}`; -function expectCodemodResult(src, expected) { - expect(codemod(prependFetchMock(src), jscodeshift)).toEqual( - prependFetchMock(expected), - ); +function expectCodemodResult( + src, + expected, + fetchMockVariableName = 'fetchMock', +) { + expect( + codemod(prependFetchMock(src, fetchMockVariableName), jscodeshift), + ).toEqual(prependFetchMock(expected, fetchMockVariableName)); } describe('codemods operating on methods', () => { describe('converting mock() to route()', () => { - //Next to the first one in a file leave a comment explaining that they need to use mockGlobal() too + //TODO Next to the first one in a file leave a comment explaining that they need to use mockGlobal() too it('single .mock()', () => { expectCodemodResult( 'fetchMock.mock("blah", 200)', @@ -62,7 +66,30 @@ describe('codemods operating on methods', () => { }); }); - // .lastUrl() => .callHistory.lastCall()?.url + describe('converting lastUrl()', () => { + it('single .lastUrl()', () => { + expectCodemodResult( + 'fetchMock.lastUrl()', + 'fetchMock.callHistory.lastCall()?.url', + ); + }); + it('lastUrl() with arguments', () => { + expectCodemodResult( + `fetchMock.lastUrl('name', {method: 'get'})`, + `fetchMock.callHistory.lastCall('name', {method: 'get'})?.url`, + ); + }); + + it('works with other names for fetch-mock', () => { + expectCodemodResult( + `fm.lastUrl('name', {method: 'get'})`, + `fm.callHistory.lastCall('name', {method: 'get'})?.url`, + 'fm', + ); + }); + }); + + // // .lastOptions() => .callHistory.lastCall()?.options // .lastResponse() => .callHistory.lastCall()?.response // .sandbox() => .fetchHandler(and maybe a comment about.createInstance()) diff --git a/packages/codemods/src/codemods/methods.js b/packages/codemods/src/codemods/methods.js index 5fbb2a61..4075b793 100644 --- a/packages/codemods/src/codemods/methods.js +++ b/packages/codemods/src/codemods/methods.js @@ -1,4 +1,4 @@ -export function simpleMethods (fetchMockVariableName, root, j) { +export function simpleMethods(fetchMockVariableName, root, j) { const fetchMockMethodCalls = root .find(j.CallExpression, { callee: { @@ -25,4 +25,25 @@ export function simpleMethods (fetchMockVariableName, root, j) { path.value.callee.property.name = 'route'; } }); + const lastUrl = root + .find(j.CallExpression, { + callee: { + object: { + type: 'Identifier', + name: fetchMockVariableName, + }, + property: { + name: 'lastUrl', + }, + }, + }) + .closest(j.ExpressionStatement); + + lastUrl.replaceWith((path) => { + const oldCall = j(path).find(j.CallExpression).get(); + const builder = j(`${fetchMockVariableName}.callHistory.lastCall()?.url`); + const newCall = builder.find(j.CallExpression).get(); + newCall.value.arguments = oldCall.value.arguments; + return builder.find(j.ExpressionStatement).get().value; + }); } diff --git a/packages/codemods/src/codemods/options.js b/packages/codemods/src/codemods/options.js index ec9aa3be..5b36de4b 100644 --- a/packages/codemods/src/codemods/options.js +++ b/packages/codemods/src/codemods/options.js @@ -1,5 +1,4 @@ -export function simpleOptions (fetchMockVariableName, root, j) { - +export function simpleOptions(fetchMockVariableName, root, j) { const configSets = root .find(j.CallExpression, { callee: { diff --git a/packages/codemods/src/index.js b/packages/codemods/src/index.js index 30004f7f..3c6089af 100644 --- a/packages/codemods/src/index.js +++ b/packages/codemods/src/index.js @@ -1,8 +1,7 @@ -import {simpleOptions} from './codemods/options' -import {simpleMethods} from './codemods/methods' +import { simpleOptions } from './codemods/options.js'; +import { simpleMethods } from './codemods/methods.js'; - -function findFetchMockVariableName (root, j) { +function findFetchMockVariableName(root, j) { let fetchMockVariableName; try { fetchMockVariableName = root @@ -23,7 +22,7 @@ function findFetchMockVariableName (root, j) { .find(j.ImportDefaultSpecifier) .get().value.local.name; } catch (err) { - throw new Error("No fetch-mock references found") + throw new Error('No fetch-mock references found', err); } } return fetchMockVariableName; @@ -31,11 +30,9 @@ function findFetchMockVariableName (root, j) { export function codemod(source, j) { const root = j(source); - const fetchMockVariableName = findFetchMockVariableName(root, j) - - simpleOptions(fetchMockVariableName, root, j) - simpleMethods(fetchMockVariableName, root, j) - + const fetchMockVariableName = findFetchMockVariableName(root, j); + simpleOptions(fetchMockVariableName, root, j); + simpleMethods(fetchMockVariableName, root, j); return root.toSource(); } diff --git a/packages/codemods/try.js b/packages/codemods/try.js new file mode 100644 index 00000000..0bb45caa --- /dev/null +++ b/packages/codemods/try.js @@ -0,0 +1,12 @@ +import { codemod } from './src/index.js'; +import jscodeshift from 'jscodeshift'; + +console.log( + codemod( + ` +import fetchMock from 'fetch-mock'; +fetchMock.lastUrl(1, 2) +`, + jscodeshift, + ), +); diff --git a/packages/codemods/try.ts b/packages/codemods/try.ts deleted file mode 100644 index 9e114a2f..00000000 --- a/packages/codemods/try.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { codemod } from './src/index'; -import jscodeshift from 'jscodeshift'; - -codemod( - ` -import fetchMock from 'fetch-mock'; -Object.assign(fetchMock.config, {overwriteRoutes: true, other: 'value'}) -Object.assign(fetchMock.config, {overwriteRoutes: true}) -`, - jscodeshift, -);