Skip to content

Commit

Permalink
test: more work on integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Sep 18, 2024
1 parent 317ede7 commit 90445a1
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"lint:ci": "eslint .",
"prettier": "prettier --cache --write *.md \"./**/*.md\" *.json \"./**/*.json\"",
"prettier:ci": "prettier *.md \"./**/*.md\" *.json \"./**/*.json\"",
"types:check": "tsc --project ./jsconfig.json && echo 'types check done'",
"types:check": "tsc --project ./tsconfig.json && echo 'types check done'",
"prepare": "husky || echo \"husky not available\"",
"build": "npm run build -w=packages",
"docs": "npm run start -w docs",
Expand Down
4 changes: 4 additions & 0 deletions packages/codemods/src/__test__/fixtures/extra-vars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const fetchMock = require('fetch-mock');
fetchMock.mock("blah", 200);
fm1.mock("blah", 200);
fm2.mock("blah", 200);
2 changes: 2 additions & 0 deletions packages/codemods/src/__test__/fixtures/jsx.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import fetchMock from 'fetch-mock';
fetchMock.mock("blah", <div>Content</div>);
4 changes: 4 additions & 0 deletions packages/codemods/src/__test__/fixtures/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import fetchMock from 'fetch-mock';
function helper (res: number): {
fetchMock.mock("blah", res)
};
31 changes: 25 additions & 6 deletions packages/codemods/src/__test__/integration.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
import { it, describe, expect } from 'vitest';
import transformer from '../index';
const util = require('node:util');
const exec = util.promisify(require('node:child_process').exec);

function expectCodemodResult(src, expected) {
expect(transformer({ source: src })).toEqual(expected);
}

describe('integration', () => {
it('allow passing in one or more variable names for fetch-mock', () => {

it('can operate on typescript', async () => {
const { stdout, stderr } = await exec('jscodeshift -d -t ./packages/codemods/src/index.js ./packages/codemods/src/__test__/fixtures/typescript.ts')
console.log({ stdout, stderr })
// expectCodemodResult(
// `import fetchMock from 'fetch-mock';
// function helper (res: number): {
// fetchMock.mock("blah", res)
// };`
// `import fetchMock from 'fetch-mock';
// function helper (res: number): {
// fetchMock.route("blah", res);
// }`,
// );
});
it('can operate on jsx', () => {
`import fetchMock from 'fetch-mock';
fetchMock.mock("blah", <div>Content</div>);`
`import fetchMock from 'fetch-mock';
fetchMock.route("blah", <div>Content</div>);`
});
it('allow passing in one or more additional variable names for fetch-mock', () => {
process.env.FM_VARIABLES = 'fm1,fm2';
expectCodemodResult(
`const fetchMock = require('fetch-mock');
Expand All @@ -18,9 +40,6 @@ fetchMock.route("blah", 200);
fm1.route("blah", 200);
fm2.route("blah", 200);`,
);
delete process.env.FM_VARIABLES
});
it('can operate on a js file', () => {});
it('can operate on a ts file', () => {});
it('can operate on a jsx file', () => {});
it('can operate on a tsx file', () => {});
});
2 changes: 1 addition & 1 deletion packages/codemods/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function codemod(source, variableName) {
return root.toSource();
}

export default function transformer(file) {
export default function transformer(file, api) {
let modifiedSource = codemod(file.source);
if (process.env.FM_VARIABLES) {
const extraVariables = process.env.FM_VARIABLES.split(',');
Expand Down

0 comments on commit 90445a1

Please sign in to comment.