Skip to content

Commit 30bf0cf

Browse files
committed
fix tests
1 parent fb799fa commit 30bf0cf

File tree

6 files changed

+126
-26
lines changed

6 files changed

+126
-26
lines changed

.changeset/grumpy-swans-think.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@codeshift/validator': minor
3+
---
4+
5+
Fundamentally simplifies and improves on how validation works.

.changeset/silly-icons-whisper.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@codeshift/types': patch
3+
---
4+
5+
Initial release

.changeset/slimy-foxes-train.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@codeshift/cli': minor
3+
---
4+
5+
Codemods can now be sourced from standalone npm packages such as react as long as they provide a codeshift.config.js. This allows for greater flexibility for where codemods may be distributed

packages/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"fs-extra": "^9.1.0",
2323
"jscodeshift": "^0.12.0",
2424
"live-plugin-manager": "^0.15.1",
25+
"lodash": "^4.17.21",
2526
"semver": "^7.3.5",
2627
"ts-node": "^9.1.1"
2728
}

packages/cli/src/main.spec.ts

Lines changed: 96 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,12 @@ jest.mock('jscodeshift/src/Runner', () => ({
66
// @ts-ignore
77
import * as jscodeshift from 'jscodeshift/src/Runner';
88
import { PluginManager } from 'live-plugin-manager';
9+
910
import main from './main';
1011

1112
const mockPath = 'src/pages/home-page/';
1213

1314
describe('main', () => {
14-
beforeEach(() => {
15-
(PluginManager as jest.Mock).mockReturnValue({
16-
install: () => Promise.resolve(undefined),
17-
require: (codemodName: string) => ({
18-
transforms: {
19-
'18.0.0': `${codemodName}/path/to/18.js`,
20-
'19.0.0': `${codemodName}/path/to/19.js`,
21-
'20.0.0': `${codemodName}/path/to/20.js`,
22-
},
23-
presets: {
24-
'update-formatting': `${codemodName}/path/to/update-formatting.js`,
25-
'update-imports': `${codemodName}/path/to/update-imports.js`,
26-
},
27-
}),
28-
uninstallAll: () => Promise.resolve(),
29-
});
30-
});
31-
3215
afterEach(() => {
3316
jest.resetAllMocks();
3417
});
@@ -134,6 +117,26 @@ describe('main', () => {
134117
});
135118

136119
describe('when running transforms with the -p flag', () => {
120+
beforeEach(() => {
121+
(PluginManager as jest.Mock).mockImplementation(() => ({
122+
install: jest.fn().mockResolvedValue(undefined),
123+
require: jest.fn().mockImplementation((codemodName: string) => {
124+
if (!codemodName.startsWith('@codeshift')) {
125+
throw new Error('Attempted to fetch codemod from npm');
126+
}
127+
128+
return {
129+
transforms: {
130+
'18.0.0': `${codemodName}/path/to/18.js`,
131+
'19.0.0': `${codemodName}/path/to/19.js`,
132+
'20.0.0': `${codemodName}/path/to/20.js`,
133+
},
134+
};
135+
}),
136+
uninstallAll: jest.fn().mockResolvedValue(undefined),
137+
}));
138+
});
139+
137140
it('should run package transform for single version', async () => {
138141
await main([mockPath], {
139142
packages: 'mylib@18.0.0',
@@ -234,6 +237,7 @@ describe('main', () => {
234237
expect.any(Object),
235238
);
236239
});
240+
237241
it('should run multiple transforms of the same package', async () => {
238242
await main([mockPath], {
239243
packages: '@myscope/mylib@20.0.0@19.0.0',
@@ -374,6 +378,25 @@ describe('main', () => {
374378
});
375379

376380
describe('when running presets with the -p flag', () => {
381+
beforeEach(() => {
382+
(PluginManager as jest.Mock).mockImplementation(() => ({
383+
install: jest.fn().mockResolvedValue(undefined),
384+
require: jest.fn().mockImplementation((codemodName: string) => {
385+
if (!codemodName.startsWith('@codeshift')) {
386+
throw new Error('Attempted to fetch codemod from npm');
387+
}
388+
389+
return {
390+
presets: {
391+
'update-formatting': `${codemodName}/path/to/update-formatting.js`,
392+
'update-imports': `${codemodName}/path/to/update-imports.js`,
393+
},
394+
};
395+
}),
396+
uninstallAll: jest.fn().mockResolvedValue(undefined),
397+
}));
398+
});
399+
377400
it('should run single preset', async () => {
378401
await main([mockPath], {
379402
packages: 'mylib#update-formatting',
@@ -508,18 +531,71 @@ describe('main', () => {
508531
});
509532
});
510533

534+
describe('when running transforms from NPM with the -p flag', () => {
535+
beforeEach(() => {
536+
(PluginManager as jest.Mock).mockImplementation(() => ({
537+
install: jest.fn().mockResolvedValue(undefined),
538+
require: jest.fn().mockImplementation((codemodName: string) => {
539+
if (codemodName.startsWith('@codeshift')) {
540+
throw new Error('Attempted to fetch codemod from community folder');
541+
}
542+
543+
return {
544+
transforms: {
545+
'18.0.0': `${codemodName}/path/to/18.js`,
546+
},
547+
presets: {
548+
'update-formatting': `${codemodName}/path/to/update-formatting.js`,
549+
},
550+
};
551+
}),
552+
uninstallAll: jest.fn().mockResolvedValue(undefined),
553+
}));
554+
});
555+
556+
it('should run package transform for single version', async () => {
557+
await main([mockPath], {
558+
packages: 'mylib@18.0.0',
559+
parser: 'babel',
560+
extensions: 'js',
561+
});
562+
563+
expect(jscodeshift.run).toHaveBeenCalledTimes(1);
564+
expect(jscodeshift.run).toHaveBeenCalledWith(
565+
'mylib/path/to/18.js',
566+
expect.arrayContaining([mockPath]),
567+
expect.anything(),
568+
);
569+
});
570+
571+
it('should run single preset', async () => {
572+
await main([mockPath], {
573+
packages: 'mylib#update-formatting',
574+
parser: 'babel',
575+
extensions: 'js',
576+
});
577+
578+
expect(jscodeshift.run).toHaveBeenCalledTimes(1);
579+
expect(jscodeshift.run).toHaveBeenCalledWith(
580+
'mylib/path/to/update-formatting.js',
581+
expect.arrayContaining([mockPath]),
582+
expect.anything(),
583+
);
584+
});
585+
});
586+
511587
describe('when reading configs using non-cjs exports', () => {
512588
it('should read configs exported with export default', async () => {
513589
(PluginManager as jest.Mock).mockReturnValue({
514590
install: () => Promise.resolve(undefined),
515591
// @ts-ignore
516-
require: (codemodName: string) => ({
592+
require: jest.fn().mockImplementationOnce((codemodName: string) => ({
517593
default: {
518594
transforms: {
519595
'18.0.0': `${codemodName}/path/to/18.js`,
520596
},
521597
},
522-
}),
598+
})),
523599
uninstallAll: () => Promise.resolve(),
524600
});
525601

packages/cli/src/main.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import { CodeshiftConfig } from '@codeshift/types';
1111
import { Flags } from './types';
1212
import { InvalidUserInputError } from './errors';
1313

14-
const packageManager = new PluginManager();
15-
16-
async function fetchCommunityPackageConfig(packageName: string) {
14+
async function fetchCommunityPackageConfig(
15+
packageName: string,
16+
packageManager: PluginManager,
17+
) {
1718
const pkgName = packageName.replace('@', '').replace('/', '__');
1819
const commPackageName = `@codeshift/mod-${pkgName}`;
1920

@@ -28,7 +29,10 @@ async function fetchCommunityPackageConfig(packageName: string) {
2829
return config;
2930
}
3031

31-
async function fetchRemotePackageConfig(packageName: string) {
32+
async function fetchRemotePackageConfig(
33+
packageName: string,
34+
packageManager: PluginManager,
35+
) {
3236
await packageManager.install(packageName);
3337
const pkg = packageManager.require(packageName);
3438

@@ -74,6 +78,7 @@ async function fetchRemotePackageConfig(packageName: string) {
7478
}
7579

7680
export default async function main(paths: string[], flags: Flags) {
81+
const packageManager = new PluginManager();
7782
let transforms: string[] = [];
7883

7984
if (!flags.transform && !flags.packages) {
@@ -104,11 +109,14 @@ export default async function main(paths: string[], flags: Flags) {
104109
let remoteConfig;
105110

106111
try {
107-
communityConfig = await fetchCommunityPackageConfig(pkgName);
112+
communityConfig = await fetchCommunityPackageConfig(
113+
pkgName,
114+
packageManager,
115+
);
108116
} catch (error) {}
109117

110118
try {
111-
remoteConfig = await fetchRemotePackageConfig(pkgName);
119+
remoteConfig = await fetchRemotePackageConfig(pkgName, packageManager);
112120
} catch (error) {}
113121

114122
if (!communityConfig && !remoteConfig) {

0 commit comments

Comments
 (0)