/
prompt-installation.test.js
92 lines (80 loc) · 3.61 KB
/
prompt-installation.test.js
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
'use strict';
jest.mock('execa');
jest.mock('cross-spawn');
const globalModulesNpmValue = 'test-npm';
jest.setMock('global-modules', globalModulesNpmValue);
jest.setMock('enquirer', {
prompt: jest.fn(),
});
jest.setMock('../run-command', jest.fn());
jest.setMock('../package-exists', jest.fn());
jest.setMock('../get-package-manager', jest.fn());
const getPackageManager = require('../get-package-manager');
const packageExists = require('../package-exists');
const promptInstallation = require('../prompt-installation');
const runCommand = require('../run-command');
const { prompt } = require('enquirer');
describe('promptInstallation', () => {
beforeAll(() => {
packageExists.mockReturnValue(true);
});
beforeEach(() => {
runCommand.mockClear();
prompt.mockClear();
});
it('should prompt to install using npm if npm is package manager', async () => {
prompt.mockReturnValue({
installConfirm: true,
});
getPackageManager.mockReturnValue('npm');
const preMessage = jest.fn();
const promptResult = await promptInstallation('test-package', preMessage);
expect(promptResult).toBeTruthy();
expect(preMessage.mock.calls.length).toEqual(1);
expect(prompt.mock.calls.length).toEqual(1);
expect(runCommand.mock.calls.length).toEqual(1);
expect(prompt.mock.calls[0][0][0].message).toMatch(/Would you like to install test-package\?/);
// install the package using npm
expect(runCommand.mock.calls[0][0]).toEqual('npm install -D test-package');
});
it('should prompt to install using yarn if yarn is package manager', async () => {
prompt.mockReturnValue({
installConfirm: true,
});
getPackageManager.mockReturnValue('yarn');
const promptResult = await promptInstallation('test-package');
expect(promptResult).toBeTruthy();
expect(prompt.mock.calls.length).toEqual(1);
expect(runCommand.mock.calls.length).toEqual(1);
expect(prompt.mock.calls[0][0][0].message).toMatch(/Would you like to install test-package\?/);
// install the package using yarn
expect(runCommand.mock.calls[0][0]).toEqual('yarn add -D test-package');
});
it('should prompt to install using pnpm if pnpm is package manager', async () => {
prompt.mockReturnValue({
installConfirm: true,
});
getPackageManager.mockReturnValue('pnpm');
const preMessage = jest.fn();
const promptResult = await promptInstallation('test-package', preMessage);
expect(promptResult).toBeTruthy();
expect(preMessage.mock.calls.length).toEqual(1);
expect(prompt.mock.calls.length).toEqual(1);
expect(runCommand.mock.calls.length).toEqual(1);
expect(prompt.mock.calls[0][0][0].message).toMatch(/Would you like to install test-package\?/);
// install the package using npm
expect(runCommand.mock.calls[0][0]).toEqual('pnpm install -D test-package');
});
it('should not install if install is not confirmed', async () => {
prompt.mockReturnValue({
installConfirm: false,
});
const promptResult = await promptInstallation('test-package');
expect(promptResult).toBeUndefined();
expect(prompt.mock.calls.length).toEqual(1);
// runCommand should not be called, because the installation is not confirmed
expect(runCommand.mock.calls.length).toEqual(0);
expect(prompt.mock.calls[0][0][0].message).toMatch(/Would you like to install test-package\?/);
expect(process.exitCode).toEqual(2);
});
});