-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathpnpapi.test.js
More file actions
299 lines (253 loc) Β· 9.07 KB
/
Copy pathpnpapi.test.js
File metadata and controls
299 lines (253 loc) Β· 9.07 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
const {npath, xfs} = require(`@yarnpkg/fslib`);
const {
fs: {writeFile, writeJson},
} = require('pkg-tests-core');
describe(`Plug'n'Play API`, () => {
test(
`it should expose VERSIONS`,
makeTemporaryEnv({}, async ({path, run, source}) => {
await run(`install`);
await expect(source(`require('pnpapi').VERSIONS`)).resolves.toMatchObject({std: 3});
}),
);
describe(`std - v1`, () => {
test(
`it should expose resolveToUnqualified`,
makeTemporaryEnv({}, async ({path, run, source}) => {
await run(`install`);
await expect(source(`typeof require('pnpapi').resolveToUnqualified`)).resolves.toEqual(`function`);
}),
);
test(
`it should expose resolveToUnqualified`,
makeTemporaryEnv({}, async ({path, run, source}) => {
await run(`install`);
await expect(source(`typeof require('pnpapi').resolveUnqualified`)).resolves.toEqual(`function`);
}),
);
test(
`it should expose resolveRequest`,
makeTemporaryEnv({}, async ({path, run, source}) => {
await run(`install`);
await expect(source(`typeof require('pnpapi').resolveRequest`)).resolves.toEqual(`function`);
}),
);
describe(`getPackageInformation`, () => {
test(
`it should return the package location`,
makeTemporaryEnv({}, async ({path, run, source}) => {
await run(`install`);
await expect(
source(`typeof require('pnpapi').getPackageInformation({name: null, reference: null}).packageLocation`),
).resolves.toEqual(
`string`,
);
}),
);
test(
`it should return the package dependencies`,
makeTemporaryEnv({
dependencies: {
[`no-deps`]: `1.0.0`,
},
}, async ({path, run, source}) => {
await run(`install`);
await expect(
source(`[...require('pnpapi').getPackageInformation({name: null, reference: null}).packageDependencies]`),
).resolves.toEqual([
[`no-deps`, `npm:1.0.0`],
]);
}),
);
});
describe(`resolveRequest`, () => {
test(
`it should return the path to the PnP file when 'pnpapi' is requested`,
makeTemporaryEnv({}, async ({path, run, source}) => {
await run(`install`);
await expect(
source(`require('pnpapi').resolveRequest('pnpapi', ${JSON.stringify(`${npath.fromPortablePath(path)}/`)})`),
).resolves.toEqual(
npath.fromPortablePath(`${path}/.pnp.js`),
);
}),
);
test(
`it should return null for builtins`,
makeTemporaryEnv({}, async ({path, run, source}) => {
await run(`install`);
await expect(
source(`require('pnpapi').resolveRequest('fs', ${JSON.stringify(`${npath.fromPortablePath(path)}/`)})`)
).resolves.toEqual(
null,
);
}),
);
test(
`it should support the 'considerBuiltins' option`,
makeTemporaryEnv(
{
dependencies: {[`fs`]: `link:./fs`},
},
async ({path, run, source}) => {
await writeFile(`${path}/fs/index.js`, `
module.exports = 'Hello world';
`);
await writeJson(`${path}/fs/package.json`, {
name: `fs`,
version: `1.0.0`,
});
await run(`install`);
await expect(
source(`require('pnpapi').resolveRequest('fs', ${JSON.stringify(`${npath.fromPortablePath(path)}/`)}, {considerBuiltins: false})`),
).resolves.toEqual(
npath.fromPortablePath(`${path}/fs/index.js`),
);
},
),
);
test(
`it should support the 'extensions' option`,
makeTemporaryEnv({}, async ({path, run, source}) => {
await writeFile(`${path}/foo.bar`, `
hello world
`);
await run(`install`);
await expect(
source(`require('pnpapi').resolveRequest('./foo', ${JSON.stringify(`${npath.fromPortablePath(path)}/`)}, {extensions: ['.bar']})`),
).resolves.toEqual(
npath.fromPortablePath(`${path}/foo.bar`),
);
}),
);
});
});
describe(`std - v2`, () => {
test(
`it should use .pnpCode to expose semantic errors`,
makeTemporaryEnv({}, async ({path, run, source}) => {
await run(`install`);
await expect(source(`(() => { try { require('pnpapi').resolveRequest('no-deps', ${JSON.stringify(`${path}/`)}) } catch (error) { return error } })()`)).resolves.toMatchObject({
pnpCode: `UNDECLARED_DEPENDENCY`,
});
}),
);
});
describe(`std - v3`, () => {
test(
`it should expose getDependencyTreeRoots`,
makeTemporaryEnv({}, async ({path, run, source}) => {
await run(`install`);
await expect(source(`typeof require('pnpapi').getDependencyTreeRoots`)).resolves.toEqual(`function`);
}),
);
describe(`getPackageInformation`, () => {
test(
`it should return the package linkage (hard link)`,
makeTemporaryEnv({
dependencies: {
[`no-deps`]: `1.0.0`,
},
}, async ({path, run, source}) => {
await run(`install`);
const reference = await source(
`require('pnpapi').getPackageInformation({name: null, reference: null}).packageDependencies.get('no-deps')`
);
await expect(
source(`require('pnpapi').getPackageInformation({name: 'no-deps', reference: ${JSON.stringify(reference)}}).linkType`),
).resolves.toEqual(
`HARD`,
);
}),
);
test(
`it should return the package linkage (soft link)`,
makeTemporaryEnv({
dependencies: {
[`self`]: `link:.`,
},
}, async ({path, run, source}) => {
await run(`install`);
const reference = await source(
`require('pnpapi').getPackageInformation({name: null, reference: null}).packageDependencies.get('self')`
);
await expect(
source(`require('pnpapi').getPackageInformation({name: 'self', reference: ${JSON.stringify(reference)}}).linkType`),
).resolves.toEqual(
`SOFT`,
);
}),
);
});
describe(`getDependencyTreeRoots`, () => {
test(
`it should return the workspaces (no children)`,
makeTemporaryEnv({
name: `my-package`,
}, async ({path, run, source}) => {
await run(`install`);
await expect(
source(`require('pnpapi').getDependencyTreeRoots()`),
).resolves.toEqual([
expect.objectContaining({name: `my-package`}),
]);
}),
);
test(
`it should return the workspaces (multiple workspaces)`,
makeTemporaryEnv({
name: `my-package`,
private: true,
workspaces: [`packages/*`],
}, async ({path, run, source}) => {
await xfs.mkdirpPromise(`${path}/packages/workspace-a`);
await xfs.writeJsonPromise(`${path}/packages/workspace-a/package.json`, {name: `workspace-a`});
await xfs.mkdirpPromise(`${path}/packages/workspace-b`);
await xfs.writeJsonPromise(`${path}/packages/workspace-b/package.json`, {name: `workspace-b`});
await run(`install`);
await expect(
source(`require('pnpapi').getDependencyTreeRoots()`),
).resolves.toEqual([
expect.objectContaining({name: `my-package`}),
expect.objectContaining({name: `workspace-a`}),
expect.objectContaining({name: `workspace-b`}),
]);
}),
);
});
});
describe(`resolveVirtual - v1`, () => {
describe(`resolveVirtual`, () => {
test(
`it should return null when the specified path isn't a virtual`,
makeTemporaryEnv({
dependencies: {
[`no-deps`]: `1.0.0`,
},
}, async ({path, run, source}) => {
await run(`install`);
await expect(
source(`require('pnpapi').resolveVirtual(require.resolve('no-deps'))`),
).resolves.toEqual(null);
}),
);
test(
`it should return the transformed path when possible`,
makeTemporaryEnv({
dependencies: {
[`peer-deps`]: `1.0.0`,
},
}, async ({path, run, source}) => {
await run(`install`);
const virtualPath = await source(`require.resolve('peer-deps')`);
// Sanity check: to ensure that the test actually tests something :)
expect(virtualPath).toMatch(`${npath.sep}$$virtual${npath.sep}`);
const physicalPath = await source(`require('pnpapi').resolveVirtual(require.resolve('peer-deps'))`);
expect(typeof physicalPath).toEqual(`string`);
expect(physicalPath).not.toEqual(virtualPath);
expect(xfs.existsSync(physicalPath)).toEqual(true);
}),
);
});
});
});