From dc2dd32284fea7e7e8fba4d11ab392b1d9801467 Mon Sep 17 00:00:00 2001 From: eps1lon Date: Thu, 19 Sep 2024 11:01:54 +0200 Subject: [PATCH 1/7] Run `@next/codemod` tests in CI --- jest.config.js | 1 + ...missing-react-import.js => add-missing-react-import.test.js} | 0 ...default-component-test.js => name-default-component.test.js} | 0 ...named-export.js => next-dynamic-access-named-export.test.js} | 0 run-tests.js | 2 +- 5 files changed, 2 insertions(+), 1 deletion(-) rename packages/next-codemod/transforms/__tests__/{add-missing-react-import.js => add-missing-react-import.test.js} (100%) rename packages/next-codemod/transforms/__tests__/{name-default-component-test.js => name-default-component.test.js} (100%) rename packages/next-codemod/transforms/__tests__/{next-dynamic-access-named-export.js => next-dynamic-access-named-export.test.js} (100%) diff --git a/jest.config.js b/jest.config.js index 4e053d770ec8c..4fd273751b1ef 100644 --- a/jest.config.js +++ b/jest.config.js @@ -12,6 +12,7 @@ const customJestConfig = { roots: [ '', '/../packages/next/src/', + '/../packages/next-codemod/', '/../packages/font/src/', ], modulePathIgnorePatterns: ['/\\.next/'], diff --git a/packages/next-codemod/transforms/__tests__/add-missing-react-import.js b/packages/next-codemod/transforms/__tests__/add-missing-react-import.test.js similarity index 100% rename from packages/next-codemod/transforms/__tests__/add-missing-react-import.js rename to packages/next-codemod/transforms/__tests__/add-missing-react-import.test.js diff --git a/packages/next-codemod/transforms/__tests__/name-default-component-test.js b/packages/next-codemod/transforms/__tests__/name-default-component.test.js similarity index 100% rename from packages/next-codemod/transforms/__tests__/name-default-component-test.js rename to packages/next-codemod/transforms/__tests__/name-default-component.test.js diff --git a/packages/next-codemod/transforms/__tests__/next-dynamic-access-named-export.js b/packages/next-codemod/transforms/__tests__/next-dynamic-access-named-export.test.js similarity index 100% rename from packages/next-codemod/transforms/__tests__/next-dynamic-access-named-export.js rename to packages/next-codemod/transforms/__tests__/next-dynamic-access-named-export.test.js diff --git a/run-tests.js b/run-tests.js index 62dd0b266fe7a..5db5926e6ce94 100644 --- a/run-tests.js +++ b/run-tests.js @@ -76,7 +76,7 @@ const TIMINGS_API_HEADERS = { const testFilters = { development: new RegExp( - '^(test/(development|e2e)|packages/.*/src/.*)/.*\\.test\\.(js|jsx|ts|tsx)$' + '^(test/(development|e2e)|packages/.*/src/.*|packages/next-codemod/.*)/.*\\.test\\.(js|jsx|ts|tsx)$' ), production: new RegExp( '^(test/(production|e2e))/.*\\.test\\.(js|jsx|ts|tsx)$' From 24aeec760acf9dbdaed57123289b383c52ceee9d Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Thu, 19 Sep 2024 13:37:47 +0200 Subject: [PATCH 2/7] fix test --- .../__testfixtures__/new-link/add-legacy-behavior.output.js | 4 ++-- .../new-link/children-interpolation.output.js | 4 ++-- .../new-link/handle-duplicate-props.output.js | 4 ++-- .../transforms/__testfixtures__/new-link/link-a.output.js | 4 ++-- .../transforms/__testfixtures__/new-link/move-props.output.js | 4 ++-- .../transforms/__testfixtures__/new-link/styled-jsx.output.js | 4 ++-- packages/next-codemod/transforms/new-link.ts | 3 +++ 7 files changed, 15 insertions(+), 12 deletions(-) diff --git a/packages/next-codemod/transforms/__testfixtures__/new-link/add-legacy-behavior.output.js b/packages/next-codemod/transforms/__testfixtures__/new-link/add-legacy-behavior.output.js index ad1d5a1a04310..17ec18b544686 100644 --- a/packages/next-codemod/transforms/__testfixtures__/new-link/add-legacy-behavior.output.js +++ b/packages/next-codemod/transforms/__testfixtures__/new-link/add-legacy-behavior.output.js @@ -6,8 +6,8 @@ function Comp({children}) { export default function Page() { return ( - + ( Home - + ) ); } \ No newline at end of file diff --git a/packages/next-codemod/transforms/__testfixtures__/new-link/children-interpolation.output.js b/packages/next-codemod/transforms/__testfixtures__/new-link/children-interpolation.output.js index b1622c24f53d4..9e310d6422209 100644 --- a/packages/next-codemod/transforms/__testfixtures__/new-link/children-interpolation.output.js +++ b/packages/next-codemod/transforms/__testfixtures__/new-link/children-interpolation.output.js @@ -8,8 +8,8 @@ const a = export default function Page() { return ( - + ( {a} - + ) ); } diff --git a/packages/next-codemod/transforms/__testfixtures__/new-link/handle-duplicate-props.output.js b/packages/next-codemod/transforms/__testfixtures__/new-link/handle-duplicate-props.output.js index 4d77892e2f23f..40cd0951b2309 100644 --- a/packages/next-codemod/transforms/__testfixtures__/new-link/handle-duplicate-props.output.js +++ b/packages/next-codemod/transforms/__testfixtures__/new-link/handle-duplicate-props.output.js @@ -2,8 +2,8 @@ import Link from 'next/link' export default function Page() { return ( - + ( Link - + ) ); } \ No newline at end of file diff --git a/packages/next-codemod/transforms/__testfixtures__/new-link/link-a.output.js b/packages/next-codemod/transforms/__testfixtures__/new-link/link-a.output.js index 4bad6108ed55e..d9477c9d53987 100644 --- a/packages/next-codemod/transforms/__testfixtures__/new-link/link-a.output.js +++ b/packages/next-codemod/transforms/__testfixtures__/new-link/link-a.output.js @@ -1,8 +1,8 @@ import Link from 'next/link' export default function Page() { return ( - + ( Link - + ) ); } \ No newline at end of file diff --git a/packages/next-codemod/transforms/__testfixtures__/new-link/move-props.output.js b/packages/next-codemod/transforms/__testfixtures__/new-link/move-props.output.js index 0587a9e7ff829..a96b7c2c74242 100644 --- a/packages/next-codemod/transforms/__testfixtures__/new-link/move-props.output.js +++ b/packages/next-codemod/transforms/__testfixtures__/new-link/move-props.output.js @@ -1,13 +1,13 @@ import Link from 'next/link' export default function Page() { return ( - { console.log('clicked') }} download> Link - + ) ); } \ No newline at end of file diff --git a/packages/next-codemod/transforms/__testfixtures__/new-link/styled-jsx.output.js b/packages/next-codemod/transforms/__testfixtures__/new-link/styled-jsx.output.js index 80f6859410b86..aee1a053d21fc 100644 --- a/packages/next-codemod/transforms/__testfixtures__/new-link/styled-jsx.output.js +++ b/packages/next-codemod/transforms/__testfixtures__/new-link/styled-jsx.output.js @@ -6,7 +6,7 @@ const CustomLink = ({ children, }) => { return ( - + ( {children} @@ -19,7 +19,7 @@ const CustomLink = ({ font-weight: 500; } `} - + ) ); }; diff --git a/packages/next-codemod/transforms/new-link.ts b/packages/next-codemod/transforms/new-link.ts index fce40964cc074..56dc61e56bcd3 100644 --- a/packages/next-codemod/transforms/new-link.ts +++ b/packages/next-codemod/transforms/new-link.ts @@ -1,3 +1,6 @@ +// It might insert extra parnes for JSX components +// x-ref: https://github.com/facebook/jscodeshift/issues/534 + import type { API, FileInfo } from 'jscodeshift' export default function transformer(file: FileInfo, api: API) { From 7ad9958a0e421bda3efbe4a5358778b698a0c10b Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Thu, 19 Sep 2024 13:55:29 +0200 Subject: [PATCH 3/7] update format --- .../next-image-experimental/general.output.tsx | 4 ++-- .../next-image-experimental/require.output.tsx | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/next-codemod/transforms/__testfixtures__/next-image-experimental/general.output.tsx b/packages/next-codemod/transforms/__testfixtures__/next-image-experimental/general.output.tsx index a63d8ba524e71..ce6e73bca0291 100644 --- a/packages/next-codemod/transforms/__testfixtures__/next-image-experimental/general.output.tsx +++ b/packages/next-codemod/transforms/__testfixtures__/next-image-experimental/general.output.tsx @@ -10,7 +10,7 @@ import img from "../public/img.jpg"; export default function Home() { const myStyle = { color: 'black' }; return ( -
+ (

Upgrade

-
+
) ); } diff --git a/packages/next-codemod/transforms/__testfixtures__/next-image-experimental/require.output.tsx b/packages/next-codemod/transforms/__testfixtures__/next-image-experimental/require.output.tsx index c546a68754afb..57000b730d003 100644 --- a/packages/next-codemod/transforms/__testfixtures__/next-image-experimental/require.output.tsx +++ b/packages/next-codemod/transforms/__testfixtures__/next-image-experimental/require.output.tsx @@ -6,11 +6,11 @@ const Foo = require("foo"); export default function Home() { return ( -
+ (

Upgrade

-
+
) ); } From 5b396a9f49513dfda531a3a5f9bc62e2a54eb715 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Thu, 19 Sep 2024 14:07:51 +0200 Subject: [PATCH 4/7] skip on ci for specific test --- .../next-image-experimental-loader.test.js | 43 +++++++++++-------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/packages/next-codemod/transforms/__tests__/next-image-experimental-loader.test.js b/packages/next-codemod/transforms/__tests__/next-image-experimental-loader.test.js index cb1bb262b5594..f73a02fe5af42 100644 --- a/packages/next-codemod/transforms/__tests__/next-image-experimental-loader.test.js +++ b/packages/next-codemod/transforms/__tests__/next-image-experimental-loader.test.js @@ -25,22 +25,27 @@ async function toObj(dir) { return obj } -it.each(readdirSync(fixtureDir))('should transform loader %s', async (loader) => { - const tmp = await mkdtemp(join(tmpdir(), `next-image-experimental-${loader}-`)) - const originalCwd = process.cwd() - try { - await mkdir(tmp, opts) - await cp(join(fixtureDir, loader, 'input'), tmp, opts) - process.chdir(tmp) - const result = await Runner.run(transform, [`.`], {}) - expect(result.error).toBe(0) - expect( - await toObj(tmp) - ).toStrictEqual( - await toObj(join(fixtureDir, loader, 'output')) - ) - } finally { - await rm(tmp, opts) - process.chdir(originalCwd) - } -}) \ No newline at end of file +if (!process.env.CI) { + it.each(readdirSync(fixtureDir))('should transform loader %', async (loader) => { + const tmp = await mkdtemp(join(tmpdir(), `next-image-experimental-${loader}-`)) + const originalCwd = process.cwd() + try { + await mkdir(tmp, opts) + await cp(join(fixtureDir, loader, 'input'), tmp, opts) + process.chdir(tmp) + const result = await Runner.run(transform, [`.`], {}) + expect(result.error).toBe(0) + expect( + await toObj(tmp) + ).toStrictEqual( + await toObj(join(fixtureDir, loader, 'output')) + ) + } finally { + await rm(tmp, opts) + process.chdir(originalCwd) + } + }) +} else { + // TODO: since this require the transform to be built first, skip on CI for now. refactor this test to use `jscodeshift` defineTest later + it('skip on ci', () => {}) +} From ff789cb975f1fe952080b59bf4b673092acc0c9f Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Thu, 19 Sep 2024 14:46:08 +0200 Subject: [PATCH 5/7] skip test --- .../__tests__/next-image-experimental-loader.test.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/next-codemod/transforms/__tests__/next-image-experimental-loader.test.js b/packages/next-codemod/transforms/__tests__/next-image-experimental-loader.test.js index f73a02fe5af42..8401b8f7bfd8e 100644 --- a/packages/next-codemod/transforms/__tests__/next-image-experimental-loader.test.js +++ b/packages/next-codemod/transforms/__tests__/next-image-experimental-loader.test.js @@ -25,8 +25,10 @@ async function toObj(dir) { return obj } -if (!process.env.CI) { - it.each(readdirSync(fixtureDir))('should transform loader %', async (loader) => { +// TODO: this is not working before it's built, re-enable on CI after migrating tests to defineTest +const loaders = readdirSync(fixtureDir) +for (const loader of loaders) { + it.skip(`should transform loader ${loader}`, async () => { const tmp = await mkdtemp(join(tmpdir(), `next-image-experimental-${loader}-`)) const originalCwd = process.cwd() try { @@ -45,7 +47,4 @@ if (!process.env.CI) { process.chdir(originalCwd) } }) -} else { - // TODO: since this require the transform to be built first, skip on CI for now. refactor this test to use `jscodeshift` defineTest later - it('skip on ci', () => {}) } From 3238b32ceaad1d88c1342a21fa5e52aefeb2483b Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Thu, 19 Sep 2024 14:53:40 +0200 Subject: [PATCH 6/7] update snapshot --- .../destructuring-this-props-nested.output.js | 4 ++-- .../url-to-withrouter/destructuring-this-props.output.js | 4 ++-- .../url-to-withrouter/first-parameter-hoc.output.js | 4 ++-- .../url-to-withrouter/with-nested-arrow-function.output.js | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/next-codemod/transforms/__testfixtures__/url-to-withrouter/destructuring-this-props-nested.output.js b/packages/next-codemod/transforms/__testfixtures__/url-to-withrouter/destructuring-this-props-nested.output.js index 8ffab0b1e6f23..97632975f54c7 100644 --- a/packages/next-codemod/transforms/__testfixtures__/url-to-withrouter/destructuring-this-props-nested.output.js +++ b/packages/next-codemod/transforms/__testfixtures__/url-to-withrouter/destructuring-this-props-nested.output.js @@ -6,7 +6,7 @@ export default withRouter(withAppContainer(withAuth( const { router } = this.props return ( - + (
router.push('/logos')} /> - + ) ); } } diff --git a/packages/next-codemod/transforms/__testfixtures__/url-to-withrouter/destructuring-this-props.output.js b/packages/next-codemod/transforms/__testfixtures__/url-to-withrouter/destructuring-this-props.output.js index 2bc886769b95c..066df6d59f918 100644 --- a/packages/next-codemod/transforms/__testfixtures__/url-to-withrouter/destructuring-this-props.output.js +++ b/packages/next-codemod/transforms/__testfixtures__/url-to-withrouter/destructuring-this-props.output.js @@ -5,7 +5,7 @@ class AddonsPage extends React.Component { router } = this.props return ( - + (
- + ) ); } } diff --git a/packages/next-codemod/transforms/__testfixtures__/url-to-withrouter/first-parameter-hoc.output.js b/packages/next-codemod/transforms/__testfixtures__/url-to-withrouter/first-parameter-hoc.output.js index 5ebb27eb27338..1d049d94053c3 100644 --- a/packages/next-codemod/transforms/__testfixtures__/url-to-withrouter/first-parameter-hoc.output.js +++ b/packages/next-codemod/transforms/__testfixtures__/url-to-withrouter/first-parameter-hoc.output.js @@ -4,7 +4,7 @@ class Plan extends React.Component { const { router} = this.props return ( - + (
- + ) ); } } diff --git a/packages/next-codemod/transforms/__testfixtures__/url-to-withrouter/with-nested-arrow-function.output.js b/packages/next-codemod/transforms/__testfixtures__/url-to-withrouter/with-nested-arrow-function.output.js index 577c51ff7db2d..fc9e3ca1a9ed8 100644 --- a/packages/next-codemod/transforms/__testfixtures__/url-to-withrouter/with-nested-arrow-function.output.js +++ b/packages/next-codemod/transforms/__testfixtures__/url-to-withrouter/with-nested-arrow-function.output.js @@ -6,7 +6,7 @@ export default withRouter(withAppContainer(withAuth( const { props, state } = this return ( -
props.router.push('/logos')} - /> + />) ); } } From 48632d91d0e1f6b9c2d16e22bdbe210fdda128ca Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Thu, 19 Sep 2024 15:15:07 +0200 Subject: [PATCH 7/7] rm empty line --- .../url-to-withrouter/first-parameter-hoc.output.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/next-codemod/transforms/__testfixtures__/url-to-withrouter/first-parameter-hoc.output.js b/packages/next-codemod/transforms/__testfixtures__/url-to-withrouter/first-parameter-hoc.output.js index 1d049d94053c3..c17c4a9a9cc13 100644 --- a/packages/next-codemod/transforms/__testfixtures__/url-to-withrouter/first-parameter-hoc.output.js +++ b/packages/next-codemod/transforms/__testfixtures__/url-to-withrouter/first-parameter-hoc.output.js @@ -11,7 +11,6 @@ class Plan extends React.Component { onLogout={() => onUser(null)} onLogoRightClick={() => Router.push('/logos')} /> -