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