/
package-manager.spec.ts
158 lines (144 loc) · 5.31 KB
/
package-manager.spec.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
import * as fs from 'fs';
import * as configModule from '../config/configuration';
import * as projectGraphFileUtils from '../project-graph/file-utils';
import {
detectPackageManager,
isWorkspacesEnabled,
modifyYarnRcToFitNewDirectory,
modifyYarnRcYmlToFitNewDirectory,
} from './package-manager';
describe('package-manager', () => {
describe('detectPackageManager', () => {
it('should detect package manager in nxJson', () => {
jest.spyOn(configModule, 'readNxJson').mockReturnValueOnce({
cli: {
packageManager: 'pnpm',
},
});
const packageManager = detectPackageManager();
expect(packageManager).toEqual('pnpm');
});
it('should detect yarn package manager from yarn.lock', () => {
jest.spyOn(configModule, 'readNxJson').mockReturnValueOnce({});
jest.spyOn(fs, 'existsSync').mockImplementation((p) => {
switch (p) {
case 'yarn.lock':
return true;
case 'pnpm-lock.yaml':
return false;
case 'package-lock.json':
return false;
default:
return jest.requireActual('fs').existsSync(p);
}
});
const packageManager = detectPackageManager();
expect(packageManager).toEqual('yarn');
expect(fs.existsSync).toHaveBeenNthCalledWith(1, 'yarn.lock');
});
it('should detect pnpm package manager from pnpm-lock.yaml', () => {
jest.spyOn(configModule, 'readNxJson').mockReturnValueOnce({});
jest.spyOn(fs, 'existsSync').mockImplementation((p) => {
switch (p) {
case 'yarn.lock':
return false;
case 'pnpm-lock.yaml':
return true;
case 'package-lock.json':
return false;
default:
return jest.requireActual('fs').existsSync(p);
}
});
const packageManager = detectPackageManager();
expect(packageManager).toEqual('pnpm');
expect(fs.existsSync).toHaveBeenCalledTimes(3);
});
it('should use npm package manager as default', () => {
jest.spyOn(configModule, 'readNxJson').mockReturnValueOnce({});
jest.spyOn(fs, 'existsSync').mockImplementation((p) => {
switch (p) {
case 'yarn.lock':
return false;
case 'pnpm-lock.yaml':
return false;
case 'package-lock.json':
return false;
default:
return jest.requireActual('fs').existsSync(p);
}
});
const packageManager = detectPackageManager();
expect(packageManager).toEqual('npm');
expect(fs.existsSync).toHaveBeenCalledTimes(5);
});
});
describe('isWorkspacesEnabled', () => {
it('should return true if package manager is pnpm and pnpm-workspace.yaml exists', () => {
jest.spyOn(fs, 'existsSync').mockReturnValueOnce(true);
expect(isWorkspacesEnabled('pnpm')).toEqual(true);
});
it('should return false if package manager is pnpm and pnpm-workspace.yaml does not exist', () => {
jest.spyOn(fs, 'existsSync').mockReturnValueOnce(false);
expect(isWorkspacesEnabled('pnpm')).toEqual(false);
});
it('should return true if package manager is yarn and workspaces exists in package.json', () => {
jest
.spyOn(projectGraphFileUtils, 'readPackageJson')
.mockReturnValueOnce({ workspaces: ['packages/*'] });
expect(isWorkspacesEnabled('yarn')).toEqual(true);
});
it('should return false if package manager is yarn and workspaces does not exist in package.json', () => {
jest
.spyOn(projectGraphFileUtils, 'readPackageJson')
.mockReturnValueOnce({});
expect(isWorkspacesEnabled('yarn')).toEqual(false);
});
it('should return true if package manager is npm and workspaces exists in package.json', () => {
jest
.spyOn(projectGraphFileUtils, 'readPackageJson')
.mockReturnValueOnce({ workspaces: ['packages/*'] });
expect(isWorkspacesEnabled('npm')).toEqual(true);
});
it('should return false if package manager is npm and workspaces does not exist in package.json', () => {
jest
.spyOn(projectGraphFileUtils, 'readPackageJson')
.mockReturnValueOnce({});
expect(isWorkspacesEnabled('npm')).toEqual(false);
});
});
describe('modifyYarnRcYmlToFitNewDirectory', () => {
it('should update paths properly', () => {
expect(
modifyYarnRcYmlToFitNewDirectory('yarnPath: ./bin/yarn.js')
).toEqual('');
});
it('should update plugins appropriately', () => {
expect(
modifyYarnRcYmlToFitNewDirectory(
[
'enableProgressBars: false',
'plugins:',
' - ./scripts/yarn-plugin.js',
' - path: .yarn/plugins/imported-plugin.js',
' spec: imported-plugin',
].join('\n')
)
).toEqual('enableProgressBars: false\n');
});
});
describe('modifyYarnRcToFitNewDirectory', () => {
it('should update paths properly', () => {
expect(modifyYarnRcToFitNewDirectory('yarn-path ./bin/yarn.js')).toEqual(
''
);
});
it('should not update other options', () => {
expect(
modifyYarnRcToFitNewDirectory(
['yarn-path ./bin/yarn.js', 'enableProgressBars false'].join('\n')
)
).toEqual('enableProgressBars false');
});
});
});