/
yarn.spec.ts
212 lines (195 loc) · 6.48 KB
/
yarn.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import {
parseYarnLockFile,
pruneYarnLockFile,
stringifyYarnLockFile,
} from './yarn';
import {
lockFile,
berryLockFile,
lockFileJustTypescript,
lockFileDevkitAndYargs,
berryLockFileDevkitAndYargs,
} from './__fixtures__/yarn.lock';
describe('yarn LockFile utility', () => {
describe('classic', () => {
const parsedLockFile = parseYarnLockFile(lockFile);
it('should parse lockfile correctly', () => {
expect(parsedLockFile.lockFileMetadata).toBeUndefined();
expect(Object.keys(parsedLockFile.dependencies).length).toEqual(324);
expect(
parsedLockFile.dependencies['@ampproject/remapping']
).toMatchSnapshot();
expect(parsedLockFile.dependencies['typescript']).toMatchSnapshot();
});
it('should map various versions of packages', () => {
expect(
Object.keys(parsedLockFile.dependencies['@jridgewell/gen-mapping'])
.length
).toEqual(2);
expect(
parsedLockFile.dependencies['@jridgewell/gen-mapping'][
'@jridgewell/gen-mapping@0.1.1'
]
).toBeDefined();
expect(
parsedLockFile.dependencies['@jridgewell/gen-mapping'][
'@jridgewell/gen-mapping@0.1.1'
].rootVersion
).toBeFalsy();
expect(
parsedLockFile.dependencies['@jridgewell/gen-mapping'][
'@jridgewell/gen-mapping@0.3.2'
]
).toBeDefined();
expect(
parsedLockFile.dependencies['@jridgewell/gen-mapping'][
'@jridgewell/gen-mapping@0.3.2'
].rootVersion
).toBeTruthy();
});
it('should map various instances of the same version', () => {
const babelCoreDependency =
parsedLockFile.dependencies['@babel/core']['@babel/core@7.19.1'];
expect(babelCoreDependency.packageMeta.length).toEqual(2);
expect(babelCoreDependency.packageMeta).toEqual([
'@babel/core@^7.11.6',
'@babel/core@^7.12.3',
]);
});
it('should match the original file on stringification', () => {
expect(stringifyYarnLockFile(parsedLockFile)).toEqual(lockFile);
});
it('shold prune the lock file', () => {
expect(
Object.keys(
pruneYarnLockFile(parsedLockFile, ['typescript']).dependencies
).length
).toEqual(1);
expect(
Object.keys(
pruneYarnLockFile(parsedLockFile, ['yargs', '@nrwl/devkit'])
.dependencies
).length
).toEqual(36);
});
it('shold correctly prune lockfile with single package', () => {
expect(
stringifyYarnLockFile(pruneYarnLockFile(parsedLockFile, ['typescript']))
).toEqual(lockFileJustTypescript);
});
it('shold correctly prune lockfile with multiple packages', () => {
expect(
stringifyYarnLockFile(
pruneYarnLockFile(parsedLockFile, ['yargs', '@nrwl/devkit'])
)
).toEqual(lockFileDevkitAndYargs);
});
});
describe('berry', () => {
const parsedLockFile = parseYarnLockFile(berryLockFile);
it('should parse lockfile correctly', () => {
expect(parsedLockFile.lockFileMetadata).toEqual({
__metadata: { cacheKey: '8', version: '6' },
workspacePackages: {
'test@workspace:.': {
dependencies: {
'@nrwl/cli': '14.7.5',
'@nrwl/workspace': '14.7.5',
nx: '14.7.5',
prettier: '^2.6.2',
typescript: '~4.8.2',
},
languageName: 'unknown',
linkType: 'soft',
resolution: 'test@workspace:.',
version: '0.0.0-use.local',
},
},
});
expect(Object.keys(parsedLockFile.dependencies).length).toEqual(386);
expect(
parsedLockFile.dependencies['@ampproject/remapping']
).toMatchSnapshot();
expect(parsedLockFile.dependencies['typescript']).toMatchSnapshot();
});
it('should map various versions of packages', () => {
expect(
Object.keys(parsedLockFile.dependencies['@jridgewell/gen-mapping'])
.length
).toEqual(2);
expect(
parsedLockFile.dependencies['@jridgewell/gen-mapping'][
'@jridgewell/gen-mapping@0.1.1'
]
).toBeDefined();
expect(
parsedLockFile.dependencies['@jridgewell/gen-mapping'][
'@jridgewell/gen-mapping@0.3.2'
]
).toBeDefined();
});
it('should map various instances of the same version', () => {
const babelCoreDependency =
parsedLockFile.dependencies['@babel/core']['@babel/core@7.19.1'];
expect(babelCoreDependency.packageMeta.length).toEqual(2);
expect(babelCoreDependency.packageMeta).toEqual([
'@babel/core@npm:^7.11.6',
'@babel/core@npm:^7.12.3',
]);
});
it('should match the original file on stringification', () => {
const result = stringifyYarnLockFile(parsedLockFile);
expect(result).toMatch(
/This file was generated by Nx. Do not edit this file directly/
);
expect(removeComment(result)).toEqual(removeComment(berryLockFile));
});
it('shold prune the lock file', () => {
expect(
Object.keys(
pruneYarnLockFile(parsedLockFile, [
'yargs',
'@nrwl/devkit',
'typescript',
]).dependencies
).length
).toEqual(37);
});
it('shold correctly prune lockfile with multiple packages', () => {
const result = stringifyYarnLockFile(
pruneYarnLockFile(parsedLockFile, [
'yargs',
'@nrwl/devkit',
'typescript',
])
);
expect(removeComment(result)).toEqual(
removeComment(berryLockFileDevkitAndYargs)
);
});
it('shold correctly prune lockfile with multiple packages and custom name', () => {
const result = pruneYarnLockFile(
parsedLockFile,
['yargs', '@nrwl/devkit', 'typescript'],
'custom-name'
);
expect(result.lockFileMetadata.workspacePackages).toMatchInlineSnapshot(`
Object {
"custom-name@workspace:^": Object {
"dependencies": Object {
"@nrwl/devkit": "14.7.5",
"typescript": "~4.8.2",
"yargs": "^17.4.0",
},
"languageName": "unknown",
"linkType": "soft",
"resolution": "custom-name@workspace:^",
"version": "0.0.0-use.local",
},
}
`);
});
});
});
// we don't care about comment message
const removeComment = (value) => value.split(/\n/).slice(2).join('\n');