/
get-package-manager.test.js
93 lines (80 loc) · 3.33 KB
/
get-package-manager.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
93
const fs = require('fs');
const path = require('path');
const syncMock = jest.fn(() => {
return {
stdout: '1.0.0',
};
});
jest.setMock('execa', {
sync: syncMock,
});
const getPackageManager = require('../get-package-manager');
jest.mock('cross-spawn');
const globalModulesNpmValue = 'test-npm';
jest.setMock('global-modules', globalModulesNpmValue);
jest.setMock('enquirer', {
prompt: jest.fn(),
});
describe('packageUtils', () => {
describe('getPackageManager', () => {
const testYarnLockPath = path.resolve(__dirname, 'test-yarn-lock');
const testNpmLockPath = path.resolve(__dirname, 'test-npm-lock');
const testPnpmLockPath = path.resolve(__dirname, 'test-pnpm-lock');
const testNpmAndPnpmPath = path.resolve(__dirname, 'test-npm-and-pnpm');
const testAllPath = path.resolve(__dirname, 'test-all-lock');
const cwdSpy = jest.spyOn(process, 'cwd');
beforeAll(() => {
// 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(testNpmAndPnpmPath, 'package-lock.json'), '');
fs.writeFileSync(path.resolve(testAllPath, 'package-lock.json'), '');
});
beforeEach(() => {
syncMock.mockClear();
});
it('should find yarn.lock', () => {
cwdSpy.mockReturnValue(testYarnLockPath);
expect(getPackageManager()).toEqual('yarn');
expect(syncMock.mock.calls.length).toEqual(0);
});
it('should find package-lock.json', () => {
cwdSpy.mockReturnValue(testNpmLockPath);
expect(getPackageManager()).toEqual('npm');
expect(syncMock.mock.calls.length).toEqual(0);
});
it('should find pnpm-lock.yaml', () => {
cwdSpy.mockReturnValue(testPnpmLockPath);
expect(getPackageManager()).toEqual('pnpm');
expect(syncMock.mock.calls.length).toEqual(0);
});
it('should prioritize npm over pnpm', () => {
cwdSpy.mockReturnValue(testNpmAndPnpmPath);
expect(getPackageManager()).toEqual('npm');
expect(syncMock.mock.calls.length).toEqual(0);
});
it('should prioritize yarn with many lock files', () => {
cwdSpy.mockReturnValue(testAllPath);
expect(getPackageManager()).toEqual('yarn');
expect(syncMock.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
cwdSpy.mockReturnValue(path.resolve(__dirname));
expect(getPackageManager()).toEqual('yarn');
expect(syncMock.mock.calls.length).toEqual(1);
});
it('should use npm if yarn command fails', () => {
syncMock.mockImplementation(() => {
throw new Error();
});
cwdSpy.mockReturnValue(path.resolve(__dirname));
expect(getPackageManager()).toEqual('npm');
expect(syncMock.mock.calls.length).toEqual(1);
});
});
});