-
-
Notifications
You must be signed in to change notification settings - Fork 591
/
index.test.ts
80 lines (69 loc) · 3.44 KB
/
index.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
'use strict';
import { packageExists, promptInstallation, runCommand } from '../';
import ExternalCommand from '../../webpack-cli/lib/commands/ExternalCommand';
describe('@webpack-cli/utils', () => {
it('should check existence of package', () => {
// use an actual path relative to the packageUtils file
expect(packageExists('../lib/package-exists')).toBeTruthy();
expect(packageExists('./nonexistent-package')).toBeFalsy();
});
it('should not throw if the user interrupts', async () => {
(promptInstallation as jest.Mock).mockImplementation(() => {
throw new Error();
});
await expect(ExternalCommand.run('info')).resolves.not.toThrow();
});
});
describe('promptInstallation', () => {
let packageUtils;
beforeAll(() => {
packageUtils = require('../lib/');
packageUtils.getPackageManager = jest.fn();
packageUtils.packageExists = jest.fn(() => true);
});
beforeEach(() => {
(runCommand as jest.Mock).mockClear();
(prompt as jest.Mock).mockClear();
});
it('should prompt to install using npm if npm is package manager', async () => {
packageUtils.getPackageManager.mockReturnValue('npm');
(prompt as jest.Mock).mockReturnValue({
installConfirm: true,
});
const preMessage = jest.fn();
const promptResult = await packageUtils.promptInstallation('test-package', preMessage);
expect(promptResult).toBeTruthy();
expect(preMessage.mock.calls.length).toEqual(1);
expect((prompt as jest.Mock).mock.calls.length).toEqual(1);
expect((runCommand as jest.Mock).mock.calls.length).toEqual(1);
expect((prompt as jest.Mock).mock.calls[0][0][0].message).toMatch(/Would you like to install test-package\?/);
// install the package using npm
expect((runCommand as jest.Mock).mock.calls[0][0]).toEqual('npm install -D test-package');
});
it('should prompt to install using yarn if yarn is package manager', async () => {
packageUtils.getPackageManager.mockReturnValue('yarn');
(prompt as jest.Mock).mockReturnValue({
installConfirm: true,
});
const promptResult = await packageUtils.promptInstallation('test-package');
expect(promptResult).toBeTruthy();
expect((prompt as jest.Mock).mock.calls.length).toEqual(1);
expect((runCommand as jest.Mock).mock.calls.length).toEqual(1);
expect((prompt as jest.Mock).mock.calls[0][0][0].message).toMatch(/Would you like to install test-package\?/);
// install the package using yarn
expect((runCommand as jest.Mock).mock.calls[0][0]).toEqual('yarn add -D test-package');
});
it('should not install if install is not confirmed', async () => {
packageUtils.getPackageManager.mockReturnValue('npm');
(prompt as jest.Mock).mockReturnValue({
installConfirm: false,
});
const promptResult = await packageUtils.promptInstallation('test-package');
expect(promptResult).toBeUndefined();
expect((prompt as jest.Mock).mock.calls.length).toEqual(1);
// runCommand should not be called, because the installation is not confirmed
expect((runCommand as jest.Mock).mock.calls.length).toEqual(0);
expect((prompt as jest.Mock).mock.calls[0][0][0].message).toMatch(/Would you like to install test-package\?/);
expect(process.exitCode).toEqual(2);
});
});