-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
NPMProxy.test.ts
208 lines (171 loc) · 6.69 KB
/
NPMProxy.test.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { NPMProxy } from './NPMProxy';
import * as PackageJsonHelper from './PackageJsonHelper';
describe('NPM Proxy', () => {
let npmProxy: NPMProxy;
beforeEach(() => {
npmProxy = new NPMProxy();
});
it('type should be npm', () => {
expect(npmProxy.type).toEqual('npm');
});
describe('initPackageJson', () => {
it('should run `npm init -y`', () => {
const executeCommandSpy = jest.spyOn(npmProxy, 'executeCommand').mockReturnValue('');
npmProxy.initPackageJson();
expect(executeCommandSpy).toHaveBeenCalledWith('npm', ['init', '-y']);
});
});
describe('installDependencies', () => {
describe('npm6', () => {
it('should run `npm install`', () => {
const executeCommandSpy = jest.spyOn(npmProxy, 'executeCommand').mockReturnValue('6.0.0');
npmProxy.installDependencies();
expect(executeCommandSpy).toHaveBeenLastCalledWith('npm', ['install'], expect.any(String));
});
});
describe('npm7', () => {
it('should run `npm install --legacy-peer-deps`', () => {
const executeCommandSpy = jest.spyOn(npmProxy, 'executeCommand').mockReturnValue('7.1.0');
npmProxy.installDependencies();
expect(executeCommandSpy).toHaveBeenLastCalledWith(
'npm',
['install', '--legacy-peer-deps'],
expect.any(String)
);
});
});
});
describe('addDependencies', () => {
describe('npm6', () => {
it('with devDep it should run `npm install -D @storybook/addons`', () => {
const executeCommandSpy = jest.spyOn(npmProxy, 'executeCommand').mockReturnValue('6.0.0');
npmProxy.addDependencies({ installAsDevDependencies: true }, ['@storybook/addons']);
expect(executeCommandSpy).toHaveBeenLastCalledWith(
'npm',
['install', '-D', '@storybook/addons'],
expect.any(String)
);
});
});
describe('npm7', () => {
it('with devDep it should run `npm install -D @storybook/addons`', () => {
const executeCommandSpy = jest.spyOn(npmProxy, 'executeCommand').mockReturnValue('7.0.0');
npmProxy.addDependencies({ installAsDevDependencies: true }, ['@storybook/addons']);
expect(executeCommandSpy).toHaveBeenLastCalledWith(
'npm',
['install', '--legacy-peer-deps', '-D', '@storybook/addons'],
expect.any(String)
);
});
});
});
describe('removeDependencies', () => {
describe('npm6', () => {
it('with devDep it should run `npm uninstall @storybook/addons`', () => {
const executeCommandSpy = jest.spyOn(npmProxy, 'executeCommand').mockReturnValue('6.0.0');
npmProxy.removeDependencies({}, ['@storybook/addons']);
expect(executeCommandSpy).toHaveBeenLastCalledWith(
'npm',
['uninstall', '@storybook/addons'],
expect.any(String)
);
});
});
describe('npm7', () => {
it('with devDep it should run `npm uninstall @storybook/addons`', () => {
const executeCommandSpy = jest.spyOn(npmProxy, 'executeCommand').mockReturnValue('7.0.0');
npmProxy.removeDependencies({}, ['@storybook/addons']);
expect(executeCommandSpy).toHaveBeenLastCalledWith(
'npm',
['uninstall', '--legacy-peer-deps', '@storybook/addons'],
expect.any(String)
);
});
});
describe('skipInstall', () => {
it('should only change package.json without running install', () => {
const executeCommandSpy = jest.spyOn(npmProxy, 'executeCommand').mockReturnValue('7.0.0');
const writePackageSpy = jest
.spyOn(PackageJsonHelper, 'writePackageJson')
.mockImplementation(jest.fn);
npmProxy.removeDependencies(
{
skipInstall: true,
packageJson: {
devDependencies: {
'@storybook/manager-webpack5': 'x.x.x',
'@storybook/react': 'x.x.x',
},
},
},
['@storybook/manager-webpack5']
);
expect(writePackageSpy).toHaveBeenCalledWith({
devDependencies: {
'@storybook/react': 'x.x.x',
},
});
expect(executeCommandSpy).not.toHaveBeenCalled();
});
});
});
describe('latestVersion', () => {
it('without constraint it returns the latest version', async () => {
const executeCommandSpy = jest.spyOn(npmProxy, 'executeCommand').mockReturnValue('"5.3.19"');
const version = await npmProxy.latestVersion('@storybook/addons');
expect(executeCommandSpy).toHaveBeenCalledWith('npm', [
'info',
'@storybook/addons',
'version',
'--json',
]);
expect(version).toEqual('5.3.19');
});
it('with constraint it returns the latest version satisfying the constraint', async () => {
const executeCommandSpy = jest
.spyOn(npmProxy, 'executeCommand')
.mockReturnValue('["4.25.3","5.3.19","6.0.0-beta.23"]');
const version = await npmProxy.latestVersion('@storybook/addons', '5.X');
expect(executeCommandSpy).toHaveBeenCalledWith('npm', [
'info',
'@storybook/addons',
'versions',
'--json',
]);
expect(version).toEqual('5.3.19');
});
it('throws an error if command output is not a valid JSON', async () => {
jest.spyOn(npmProxy, 'executeCommand').mockReturnValue('NOT A JSON');
await expect(npmProxy.latestVersion('@storybook/addons')).rejects.toThrow();
});
});
describe('getVersion', () => {
it('with a Storybook package listed in versions.json it returns the version', async () => {
// eslint-disable-next-line global-require
const storybookAngularVersion = require('../versions').default['@storybook/angular'];
const executeCommandSpy = jest.spyOn(npmProxy, 'executeCommand').mockReturnValue('"5.3.19"');
const version = await npmProxy.getVersion('@storybook/angular');
expect(executeCommandSpy).toHaveBeenCalledWith('npm', [
'info',
'@storybook/angular',
'version',
'--json',
]);
expect(version).toEqual(`^${storybookAngularVersion}`);
});
it('with a Storybook package not listed in versions.json it returns the latest version', async () => {
const packageVersion = '5.3.19';
const executeCommandSpy = jest
.spyOn(npmProxy, 'executeCommand')
.mockReturnValue(`"${packageVersion}"`);
const version = await npmProxy.getVersion('@storybook/react-native');
expect(executeCommandSpy).toHaveBeenCalledWith('npm', [
'info',
'@storybook/react-native',
'version',
'--json',
]);
expect(version).toEqual(`^${packageVersion}`);
});
});
});