/
packageUtils.test.ts
183 lines (159 loc) · 7.42 KB
/
packageUtils.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
'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('../lib/processUtils', {
runCommand: jest.fn(),
});
import fs from 'fs';
import path from 'path';
import execa from 'execa';
import spawn from 'cross-spawn';
import { prompt } from 'enquirer';
import { getPackageManager, packageExists } from '../lib/packageUtils';
import { runCommand } from '../lib/processUtils';
describe('packageUtils', () => {
describe('getPackageManager', () => {
const testYarnLockPath = path.resolve(__dirname, 'test-yarn-lock');
const testNpmLockPath = path.resolve(__dirname, 'test-npm-lock');
const testBothPath = path.resolve(__dirname, 'test-both');
const cwdSpy = jest.spyOn(process, 'cwd');
beforeAll(() => {
// mock sync
execa.sync = jest.fn();
// package-lock.json is ignored by .gitignore, so we simply
// write a lockfile here for testing
if (!fs.existsSync(testNpmLockPath)) {
fs.mkdirSync(testNpmLockPath);
}
fs.writeFileSync(path.resolve(testNpmLockPath, 'package-lock.json'), '');
fs.writeFileSync(path.resolve(testBothPath, 'package-lock.json'), '');
});
beforeEach(() => {
(execa.sync as jest.Mock).mockClear();
});
it('should find yarn.lock', () => {
cwdSpy.mockReturnValue(testYarnLockPath);
expect(getPackageManager()).toEqual('yarn');
expect((execa.sync as jest.Mock).mock.calls.length).toEqual(0);
});
it('should find package-lock.json', () => {
cwdSpy.mockReturnValue(testNpmLockPath);
expect(getPackageManager()).toEqual('npm');
expect((execa.sync as jest.Mock).mock.calls.length).toEqual(0);
});
it('should prioritize yarn with many lock files', () => {
cwdSpy.mockReturnValue(testBothPath);
expect(getPackageManager()).toEqual('yarn');
expect((execa.sync as jest.Mock).mock.calls.length).toEqual(0);
});
it('should use yarn if yarn command works', () => {
// yarn should output a version number to stdout if
// it is installed
(execa.sync as jest.Mock).mockImplementation(() => {
return {
stdout: '1.0.0',
};
});
cwdSpy.mockReturnValue(path.resolve(__dirname));
expect(getPackageManager()).toEqual('yarn');
expect((execa.sync as jest.Mock).mock.calls.length).toEqual(1);
});
it('should use npm if yarn command fails', () => {
(execa.sync as jest.Mock).mockImplementation(() => {
throw new Error();
});
cwdSpy.mockReturnValue(path.resolve(__dirname));
expect(getPackageManager()).toEqual('npm');
expect((execa.sync as jest.Mock).mock.calls.length).toEqual(1);
});
});
describe('getPathToGlobalPackages', () => {
let packageUtils;
beforeAll(() => {
packageUtils = require('../lib/packageUtils');
packageUtils.getPackageManager = jest.fn();
});
it('uses global-modules if package manager is npm', () => {
packageUtils.getPackageManager.mockReturnValue('npm');
expect(packageUtils.getPathToGlobalPackages()).toEqual(globalModulesNpmValue);
});
it('executes a command to find yarn global dir if package manager is yarn', () => {
packageUtils.getPackageManager.mockReturnValue('yarn');
(spawn.sync as jest.Mock).mockReturnValue({
stdout: {
toString: (): string => {
return 'test-yarn';
},
},
});
// after the yarn global dir is found, the node_modules directory
// is added on to the path
expect(packageUtils.getPathToGlobalPackages()).toEqual(`test-yarn${path.sep}node_modules`);
});
});
describe('packageExists', () => {
it('should check existence of package', () => {
// use an actual path relative to the packageUtils file
expect(packageExists('./processUtils')).toBeTruthy();
expect(packageExists('./nonexistent-package')).toBeFalsy();
});
});
describe('promptInstallation', () => {
let packageUtils;
beforeAll(() => {
packageUtils = require('../lib/packageUtils');
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);
});
});
});