Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
add windows unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DmytroHryshyn committed Jan 17, 2024
1 parent 66baca3 commit 7245639
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 134 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest, ubuntu-latest]
os: [macos-latest, ubuntu-latest, windows-latest]

steps:
- name: Check out Git repository
Expand All @@ -34,4 +34,9 @@ jobs:
run: pnpm install

- name: Run codemod unit tests
run: pnpm test
run: |
if [ "${{ matrix.os }}" == "windows-latest" ]; then
pnpm test:win
else
pnpm test
fi
132 changes: 0 additions & 132 deletions codemods/next/13/app-directory-boilerplate/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,136 +210,6 @@ describe('next 13 app-directory-boilerplate', function () {
);
});

// Windows
// it.only('should build correct files', async function () {
// const externalFileCommands = await transform({
// 'C:\\project\\pages\\index.jsx': INDEX_CONTENT,
// 'C:\\project\\pages\\_app.jsx': 'any',
// 'C:\\project\\pages\\app.jsx': 'any',
// 'C:\\project\\pages\\_document.jsx': 'any',
// 'C:\\project\\pages\\_error.jsx': 'any',
// 'C:\\project\\pages\\_404.jsx': 'any',
// 'C:\\project\\pages\\[a]\\[b].tsx': A_B_CONTENT,
// 'C:\\project\\pages\\[a]\\c.tsx': A_C_CONTENT,
// 'C:\\project\\pages\\a\\index.tsx': 'any',
// });

// console.log(JSON.stringify(externalFileCommands, null, 2), '???commands')
// deepStrictEqual(externalFileCommands.length, 18);

// ok(
// externalFileCommands.some(
// (command) =>
// command.kind === 'deleteFile' &&
// command.path === 'C:\\project\\pages\\_app.jsx',
// ),
// );

// ok(
// externalFileCommands.some(
// (command) =>
// command.kind === 'deleteFile' &&
// command.path === 'C:\\project\\pages\\_document.jsx',
// ),
// );

// ok(
// externalFileCommands.some(
// (command) => command.path === 'C:\\project\\app\\layout.tsx',
// ),
// );

// ok(
// externalFileCommands.some(
// (command) => command.path === 'C:\\project\\app\\error.tsx',
// ),
// );

// ok(
// externalFileCommands.some(
// (command) => command.path === 'C:\\project\\app\\not-found.tsx',
// ),
// );

// ok(
// externalFileCommands.some(
// (command) => command.path === 'C:\\project\\app\\page.tsx',
// ),
// );

// ok(
// externalFileCommands.some(
// (command) =>
// command.path === 'C:\\project\\app\\[a]\\[b]\\page.tsx',
// ),
// );

// ok(
// externalFileCommands.some(
// (command) => command.path === 'C:\\project\\app\\[a]\\c\\page.tsx',
// ),
// );

// ok(
// externalFileCommands.some(
// (command) => command.path === 'C:\\project\\app\\a\\page.tsx',
// ),
// );

// ok(
// externalFileCommands.some((command) => {
// return (
// command.kind === 'upsertFile' &&
// command.path === 'C:\\project\\app\\components.tsx' &&
// command.data.replace(/\W/gm, '') ===
// `
// 'use client';
// // This file has been sourced from: C\\project\\pages\\index.jsx

// export default function Index({}) {
// return null;
// }
// ;`.replace(/\W/gm, '')
// );
// }),
// );

// ok(
// externalFileCommands.some((command) => {
// return (
// command.kind === 'upsertFile' &&
// command.path === 'C:\\project\\app\\[a]\\c\\page.tsx' &&
// command.data.replace(/\W/gm, '') ===
// `
// // This file has been sourced from: C"\\project\\pages\\[a]\\c.tsx
// import Components from "./components";
// // TODO reimplement getServerSideProps with custom logic
// const getServerSideProps = () => {
// };
// export default async function Page(props: any) {
// return <Components {...props}/>;
// }
// `.replace(/\W/gm, '')
// );
// }),
// );

// ok(
// externalFileCommands.some((command) => {
// return (
// command.kind === 'upsertFile' &&
// command.path ===
// 'C:\\project\\app\\[a]\\[b]\\components.tsx' &&
// command.data.replace(/\W/gm, '') ===
// `
// 'use client';
// // This file has been sourced from: C:\\project\\pages\\[a]\\[b].tsx
// `.replace(/\W/gm, '')
// );
// }),
// );
// });

it('migrated page should keep only data-fetching hooks and wrapped client component', async function () {
const INDEX_CONTENT = `'
const Index = () => '';
Expand Down Expand Up @@ -818,8 +688,6 @@ describe('next 13 app-directory-boilerplate', function () {
'/opt/project/pages/a/b/c.tsx': index,
});

console.log([upsertPageCommand, _, deleteFileCommand], '???');

deepStrictEqual(upsertPageCommand?.kind, 'upsertFile');
deepStrictEqual(
upsertPageCommand?.path,
Expand Down
214 changes: 214 additions & 0 deletions codemods/next/13/app-directory-boilerplate/test/test.win.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
import { deepStrictEqual, ok } from 'node:assert';
import { describe, it } from 'vitest';
import { DirectoryJSON, Volume, createFsFromVolume } from 'memfs';
import { buildApi, executeFilemod } from '@intuita-inc/filemod';
import { repomod } from '../src/index.js';
import tsmorph from 'ts-morph';
import { fromMarkdown } from 'mdast-util-from-markdown';
import { toMarkdown } from 'mdast-util-to-markdown';
import { mdxjs } from 'micromark-extension-mdxjs';
import { mdxFromMarkdown, mdxToMarkdown } from 'mdast-util-mdx';
import { visit } from 'unist-util-visit';
import {
buildUnifiedFileSystem,
buildPathAPI,
} from '@codemod-registry/utilities';

const INDEX_CONTENT = `
import A from './testQWE';
export default function Index({}) {
return null;
}
export const getStaticProps = async ({}) => {
return {
props: {},
revalidate: 10,
}
}
`;

const A_B_CONTENT = `
import { X } from "../../testABC";
import { Y } from "./testDEF";
export const getStaticPaths = () => {
}
`;

const A_C_CONTENT = `
export const getServerSideProps = () => {
}
`;

const transform = async (json: DirectoryJSON) => {
const volume = Volume.fromJSON(json);

const fs = createFsFromVolume(volume);

const unifiedFileSystem = buildUnifiedFileSystem(fs);
const pathApi = buildPathAPI('/');

const parseMdx = (data: string) =>
fromMarkdown(data, {
extensions: [mdxjs()],
mdastExtensions: [mdxFromMarkdown()],
});

type Root = ReturnType<typeof fromMarkdown>;

const stringifyMdx = (tree: Root) =>
toMarkdown(tree, { extensions: [mdxToMarkdown()] });

const api = buildApi<{
tsmorph: typeof tsmorph;
parseMdx: typeof parseMdx;
stringifyMdx: typeof stringifyMdx;
visitMdxAst: typeof visit;
}>(
unifiedFileSystem,
() => ({
tsmorph,
parseMdx,
stringifyMdx,
visitMdxAst: visit,
}),
pathApi,
);

return executeFilemod(api, repomod, '/', {}, {});
};

describe('next 13 app-directory-boilerplate', () => {
it('should build correct files', async function () {
const externalFileCommands = await transform({
'C:\\project\\pages\\index.jsx': INDEX_CONTENT,
'C:\\project\\pages\\_app.jsx': 'any',
'C:\\project\\pages\\app.jsx': 'any',
'C:\\project\\pages\\_document.jsx': 'any',
'C:\\project\\pages\\_error.jsx': 'any',
'C:\\project\\pages\\_404.jsx': 'any',
'C:\\project\\pages\\[a]\\[b].tsx': A_B_CONTENT,
'C:\\project\\pages\\[a]\\c.tsx': A_C_CONTENT,
'C:\\project\\pages\\a\\index.tsx': 'any',
});

deepStrictEqual(externalFileCommands.length, 18);

ok(
externalFileCommands.some(
(command) =>
command.kind === 'deleteFile' &&
command.path === 'C:\\project\\pages\\_app.jsx',
),
);

ok(
externalFileCommands.some(
(command) =>
command.kind === 'deleteFile' &&
command.path === 'C:\\project\\pages\\_document.jsx',
),
);

ok(
externalFileCommands.some(
(command) => command.path === 'C:\\project\\app\\layout.tsx',
),
);

ok(
externalFileCommands.some(
(command) => command.path === 'C:\\project\\app\\error.tsx',
),
);

ok(
externalFileCommands.some(
(command) => command.path === 'C:\\project\\app\\not-found.tsx',
),
);

ok(
externalFileCommands.some(
(command) => command.path === 'C:\\project\\app\\page.tsx',
),
);

ok(
externalFileCommands.some(
(command) =>
command.path === 'C:\\project\\app\\[a]\\[b]\\page.tsx',
),
);

ok(
externalFileCommands.some(
(command) =>
command.path === 'C:\\project\\app\\[a]\\c\\page.tsx',
),
);

ok(
externalFileCommands.some(
(command) => command.path === 'C:\\project\\app\\a\\page.tsx',
),
);

ok(
externalFileCommands.some((command) => {
return (
command.kind === 'upsertFile' &&
command.path === 'C:\\project\\app\\components.tsx' &&
command.data.replace(/\W/gm, '') ===
`
'use client';
// This file has been sourced from: C\\project\\pages\\index.jsx
export default function Index({}) {
return null;
}
;`.replace(/\W/gm, '')
);
}),
);

ok(
externalFileCommands.some((command) => {
return (
command.kind === 'upsertFile' &&
command.path === 'C:\\project\\app\\[a]\\c\\page.tsx' &&
command.data.replace(/\W/gm, '') ===
`
// This file has been sourced from: C"\\project\\pages\\[a]\\c.tsx
import Components from "./components";
// TODO reimplement getServerSideProps with custom logic
const getServerSideProps = () => {
};
export default async function Page(props: any) {
return <Components {...props}/>;
}
`.replace(/\W/gm, '')
);
}),
);

ok(
externalFileCommands.some((command) => {
return (
command.kind === 'upsertFile' &&
command.path ===
'C:\\project\\app\\[a]\\[b]\\components.tsx' &&
command.data.replace(/\W/gm, '') ===
`
'use client';
// This file has been sourced from: C:\\project\\pages\\[a]\\[b].tsx
`.replace(/\W/gm, '')
);
}),
);
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"lint:eslint": "eslint --fix .",
"lint:prettier": "prettier --write .",
"test": "vitest run",
"test:win": "vitest run test.win.ts",
"coverage": "vitest run --coverage"
},
"devDependencies": {
Expand Down

0 comments on commit 7245639

Please sign in to comment.