Skip to content

Commit 5133c81

Browse files
committed
adds tests
1 parent 82638ab commit 5133c81

File tree

3 files changed

+191
-27
lines changed

3 files changed

+191
-27
lines changed

.changeset/bright-kangaroos-tease.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@codeshift/cli': patch
3+
---
4+
5+
Minor fix to list command

packages/cli/src/list.spec.ts

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
jest.mock('live-plugin-manager');
2+
3+
import chalk from 'chalk';
4+
import { PluginManager } from 'live-plugin-manager';
5+
6+
import list from './list';
7+
8+
describe('list', () => {
9+
beforeEach(() => {
10+
jest.spyOn(console, 'log').mockImplementation();
11+
jest.spyOn(console, 'warn').mockImplementation();
12+
13+
(PluginManager as jest.Mock).mockImplementation(() => ({
14+
install: jest.fn().mockResolvedValue(undefined),
15+
require: jest.fn().mockImplementation(() => ({
16+
transforms: {
17+
'18.0.0': 'path/to/18.js',
18+
'19.0.0': 'path/to/19.js',
19+
},
20+
presets: {
21+
'sort-imports': 'path/to/sort-imports.js',
22+
},
23+
})),
24+
}));
25+
});
26+
afterEach(() => {
27+
jest.resetAllMocks();
28+
});
29+
30+
it('should list codemods for single valid package', async () => {
31+
await list(['foobar']);
32+
33+
const result = (console.log as jest.Mock).mock.calls.join('\n');
34+
35+
expect(result).toEqual(`${chalk.bold('foobar')}
36+
├─ transforms
37+
| ├─ 18.0.0
38+
| └─ 19.0.0
39+
└─ presets
40+
└─ sort-imports`);
41+
expect(console.warn).not.toHaveBeenCalled();
42+
});
43+
44+
it('should list codemods for single valid scoped package', async () => {
45+
await list(['@foo/bar']);
46+
47+
const result = (console.log as jest.Mock).mock.calls.join('\n');
48+
49+
expect(result).toEqual(`${chalk.bold('@foo/bar')}
50+
├─ transforms
51+
| ├─ 18.0.0
52+
| └─ 19.0.0
53+
└─ presets
54+
└─ sort-imports`);
55+
expect(console.warn).not.toHaveBeenCalled();
56+
});
57+
58+
it('should list codemods for multiple packages', async () => {
59+
await list(['bar', '@foo/bar']);
60+
61+
const result = (console.log as jest.Mock).mock.calls.join('\n');
62+
63+
expect(result).toEqual(`${chalk.bold('bar')}
64+
├─ transforms
65+
| ├─ 18.0.0
66+
| └─ 19.0.0
67+
└─ presets
68+
└─ sort-imports
69+
${chalk.bold('@foo/bar')}
70+
├─ transforms
71+
| ├─ 18.0.0
72+
| └─ 19.0.0
73+
└─ presets
74+
└─ sort-imports`);
75+
expect(console.warn).not.toHaveBeenCalled();
76+
});
77+
78+
it('should return error message a package is not found', async () => {
79+
(PluginManager as jest.Mock).mockImplementation(() => ({
80+
install: jest.fn().mockImplementation(() => {
81+
throw new Error('404 not found');
82+
}),
83+
}));
84+
85+
await list(['bar']);
86+
87+
expect(console.warn).toHaveBeenCalledWith(
88+
chalk.red(`Unable to find codeshift package: ${chalk.bold('bar')}.`),
89+
);
90+
expect(console.warn).toHaveBeenCalledTimes(1);
91+
});
92+
93+
it('should return error message for multiple packages that are not found', async () => {
94+
(PluginManager as jest.Mock).mockImplementation(() => ({
95+
install: jest.fn().mockImplementation(() => {
96+
throw new Error('404 not found');
97+
}),
98+
}));
99+
100+
await list(['bar', '@foo/bar']);
101+
102+
expect(console.warn).toHaveBeenCalledWith(
103+
chalk.red(`Unable to find codeshift package: ${chalk.bold('bar')}.`),
104+
);
105+
expect(console.warn).toHaveBeenCalledWith(
106+
chalk.red(`Unable to find codeshift package: ${chalk.bold('@foo/bar')}.`),
107+
);
108+
expect(console.warn).toHaveBeenCalledTimes(2);
109+
});
110+
111+
it('should continue if one or more packages are not found', async () => {
112+
(PluginManager as jest.Mock).mockImplementation(() => ({
113+
install: jest
114+
.fn()
115+
.mockImplementationOnce(() => {
116+
throw new Error('404 not found');
117+
})
118+
.mockResolvedValueOnce(undefined)
119+
.mockResolvedValueOnce(undefined)
120+
.mockImplementationOnce(() => {
121+
throw new Error('404 not found');
122+
})
123+
.mockResolvedValueOnce(undefined)
124+
.mockResolvedValueOnce(undefined),
125+
require: jest.fn().mockImplementation(() => ({
126+
transforms: {
127+
'18.0.0': 'path/to/18.js',
128+
'19.0.0': 'path/to/19.js',
129+
},
130+
presets: {
131+
'sort-imports': 'path/to/sort-imports.js',
132+
},
133+
})),
134+
}));
135+
136+
await list(['unknown', 'found1', 'dunno', 'found2']);
137+
138+
const result = (console.log as jest.Mock).mock.calls.join('\n');
139+
140+
expect(result).toEqual(`${chalk.bold('found1')}
141+
├─ transforms
142+
| ├─ 18.0.0
143+
| └─ 19.0.0
144+
└─ presets
145+
└─ sort-imports
146+
${chalk.bold('found2')}
147+
├─ transforms
148+
| ├─ 18.0.0
149+
| └─ 19.0.0
150+
└─ presets
151+
└─ sort-imports`);
152+
153+
expect(console.warn).toHaveBeenCalledWith(
154+
chalk.red(`Unable to find codeshift package: ${chalk.bold('unknown')}.`),
155+
);
156+
expect(console.warn).toHaveBeenCalledWith(
157+
chalk.red(`Unable to find codeshift package: ${chalk.bold('dunno')}.`),
158+
);
159+
expect(console.warn).toHaveBeenCalledTimes(2);
160+
});
161+
});

packages/cli/src/list.ts

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,53 @@
11
import chalk from 'chalk';
22
import { PluginManager } from 'live-plugin-manager';
3+
import { CodeshiftConfig } from '@codeshift/types';
34

45
export default async function list(packages: string[]) {
56
const packageManager = new PluginManager();
67

7-
for (const pkg of packages) {
8-
const pkgSplit = pkg.split('@').filter(str => !!str);
8+
for (const packageName of packages) {
9+
const pkgSplit = packageName.split('@').filter(str => !!str);
910
const name = pkgSplit[0].replace('/', '__');
1011
const codemodName = `@codeshift/mod-${name}`;
1112

1213
try {
1314
await packageManager.install(codemodName);
1415
} catch (error) {
15-
console.log(
16+
console.warn(
1617
chalk.red(
17-
`Unable to find codeshift package: ${chalk.bold(codemodName)}.`,
18+
`Unable to find codeshift package: ${chalk.bold(packageName)}.`,
1819
),
1920
);
2021

21-
return;
22+
continue;
2223
}
2324

2425
await packageManager.install(codemodName);
25-
const { default: codeshiftConfig } = packageManager.require(codemodName);
26+
const pkg = packageManager.require(codemodName);
27+
const config: CodeshiftConfig = pkg.default ? pkg.default : pkg;
2628

27-
console.log(chalk.bold(pkg));
29+
console.log(chalk.bold(packageName));
2830

29-
if (codeshiftConfig.transforms) {
31+
if (config.transforms) {
3032
console.log(`├─ transforms`),
31-
Object.keys(codeshiftConfig.transforms).forEach(
32-
(transform, index, array) => {
33-
if (index + 1 === array.length) {
34-
console.log(`| └─ ${transform}`);
35-
return;
36-
}
37-
console.log(`| ├─ ${transform}`);
38-
},
39-
);
33+
Object.keys(config.transforms).forEach((transform, index, array) => {
34+
if (index + 1 === array.length) {
35+
console.log(`| └─ ${transform}`);
36+
return;
37+
}
38+
console.log(`| ├─ ${transform}`);
39+
});
4040
}
4141

42-
if (codeshiftConfig.presets) {
42+
if (config.presets) {
4343
console.log(`└─ presets`),
44-
Object.keys(codeshiftConfig.presets).forEach(
45-
(transform, index, array) => {
46-
if (index + 1 === array.length) {
47-
console.log(` └─ ${transform}`);
48-
return;
49-
}
50-
console.log(`| ├─ ${transform}`);
51-
},
52-
);
44+
Object.keys(config.presets).forEach((transform, index, array) => {
45+
if (index + 1 === array.length) {
46+
console.log(` └─ ${transform}`);
47+
return;
48+
}
49+
console.log(`| ├─ ${transform}`);
50+
});
5351
}
5452
}
5553
}

0 commit comments

Comments
 (0)