-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
pnp-esm.test.ts
344 lines (302 loc) Β· 9.61 KB
/
pnp-esm.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import {Filename, PortablePath, ppath, xfs} from '@yarnpkg/fslib';
describe(`Plug'n'Play - ESM`, () => {
test(
`it should be able to import a dependency`,
makeTemporaryEnv(
{
type: `module`,
dependencies: {
"no-deps": `1.0.0`,
},
},
async ({path, run, source}) => {
await expect(run(`install`)).resolves.toMatchObject({code: 0});
await xfs.writeFilePromise(
ppath.join(path, `index.js` as Filename),
`import noDeps from 'no-deps/index.js';\nconsole.log(noDeps)`,
);
await expect(run(`node`, `./index.js`)).resolves.toMatchObject({
code: 0,
stdout: `{ name: 'no-deps', version: '1.0.0' }\n`,
});
},
),
);
test(
`it should support relative imports`,
makeTemporaryEnv(
{
type: `module`,
},
async ({path, run, source}) => {
await expect(run(`install`)).resolves.toMatchObject({code: 0});
await xfs.writeFilePromise(
ppath.join(path, `index.js` as Filename),
`import foo from './foo.js';\nconsole.log(foo)`,
);
await xfs.writeFilePromise(ppath.join(path, `foo.js` as Filename), `export default 42`);
await expect(run(`node`, `./index.js`)).resolves.toMatchObject({
code: 0,
stdout: `42\n`,
});
},
),
);
test(
`it should not resolve extensions`,
makeTemporaryEnv(
{
type: `module`,
},
async ({path, run, source}) => {
await expect(run(`install`)).resolves.toMatchObject({code: 0});
await xfs.writeFilePromise(
ppath.join(path, `index.js` as Filename),
`import './foo';`,
);
await xfs.writeFilePromise(ppath.join(path, `foo.js` as Filename), ``);
await expect(run(`node`, `./index.js`)).rejects.toMatchObject({
code: 1,
stderr: expect.stringContaining(
`none of those files can be found on the disk`,
),
});
},
),
);
test(
`it should not resolve JSON files`,
makeTemporaryEnv(
{
type: `module`,
},
async ({path, run, source}) => {
await expect(run(`install`)).resolves.toMatchObject({code: 0});
await xfs.writeFilePromise(
ppath.join(path, `index.js` as Filename),
`import './foo.json';`,
);
await xfs.writeFilePromise(ppath.join(path, `foo.json` as Filename), `{"name": "foo"}`);
await expect(run(`node`, `./index.js`)).rejects.toMatchObject({
code: 1,
stderr: expect.stringContaining(`Unknown file extension`),
});
},
),
);
test(
`it should support named exports in commonjs files`,
makeTemporaryEnv(
{
dependencies: {
'no-deps-exports': `1.0.0`,
},
type: `module`,
},
async ({path, run, source}) => {
await expect(run(`install`)).resolves.toMatchObject({code: 0});
await xfs.writeFilePromise(
ppath.join(path, `index.js` as Filename),
`import {foo} from 'no-deps-exports';\nconsole.log(foo)`,
);
await expect(run(`node`, `./index.js`)).resolves.toMatchObject({
code: 0,
stdout: `42\n`,
});
},
),
);
test(
`it should always set default as module.exports when importing a commonjs file`,
makeTemporaryEnv(
{
type: `module`,
},
async ({path, run, source}) => {
await expect(run(`install`)).resolves.toMatchObject({code: 0});
await xfs.writeFilePromise(
ppath.join(path, `index.js` as Filename),
`import foo from './foo.cjs';\nconsole.log(foo)`,
);
await xfs.writeFilePromise(
ppath.join(path, `foo.cjs` as Filename),
`module.exports.default = 42`,
);
await expect(run(`node`, `./index.js`)).resolves.toMatchObject({
code: 0,
stdout: `{ default: 42 }\n`,
});
},
),
);
test(
`it should respect exports`,
makeTemporaryEnv(
{
type: `module`,
dependencies: {
foo: `portal:./pkg`,
},
},
async ({path, run, source}) => {
await xfs.mkdirPromise(ppath.join(path, `pkg` as Filename));
await xfs.writeJsonPromise(ppath.join(path, `pkg/package.json` as Filename), {
exports: {
import: `./foo.mjs`,
},
});
await xfs.writeFilePromise(ppath.join(path, `pkg/foo.mjs` as Filename), `export default 42`);
await xfs.writeFilePromise(ppath.join(path, `index.js` as Filename), `import foo from 'foo';\nconsole.log(foo)`);
await expect(run(`install`)).resolves.toMatchObject({code: 0});
await expect(run(`node`, `./index.js`)).resolves.toMatchObject({
code: 0,
stdout: `42\n`,
});
},
),
);
test(
`it should use legacy resolve when exports is missing`,
makeTemporaryEnv(
{
type: `module`,
dependencies: {
foo: `portal:./pkg`,
},
},
async ({path, run, source}) => {
await xfs.mkdirPromise(ppath.join(path, `pkg` as Filename));
await xfs.writeJsonPromise(ppath.join(path, `pkg/package.json` as Filename), {});
await xfs.writeFilePromise(ppath.join(path, `pkg/index.js` as Filename), `module.exports = 42`);
await xfs.writeFilePromise(ppath.join(path, `index.js` as Filename), `import foo from 'foo';\nconsole.log(foo)`);
await expect(run(`install`)).resolves.toMatchObject({code: 0});
await expect(run(`node`, `./index.js`)).resolves.toMatchObject({
code: 0,
stdout: `42\n`,
});
},
),
);
test(
`it should not allow unknown extensions`,
makeTemporaryEnv(
{
type: `module`,
},
async ({path, run, source}) => {
await xfs.writeFilePromise(ppath.join(path, `index` as Filename), `console.log('foo')`);
await expect(run(`install`)).resolves.toMatchObject({code: 0});
await expect(run(`node`, `./index`)).rejects.toMatchObject({
code: 1,
stderr: expect.stringContaining(`Unknown file extension ""`),
});
},
),
);
// Tests the workaround for https://github.com/nodejs/node/issues/33226
test(
`it should not enter ESM mode just because the loader is present`,
makeTemporaryEnv(
{ },
{
pnpEnableExperimentalEsm: true,
},
async ({path, run, source}) => {
await xfs.writeFilePromise(ppath.join(path, `index` as Filename), `console.log(typeof require === 'undefined')`);
await expect(run(`install`)).resolves.toMatchObject({code: 0});
await expect(run(`node`, `./index`)).resolves.toMatchObject({
code: 0,
stdout: `false\n`,
});
},
),
);
test(
`it should enter ESM mode when entrypoint is ESM`,
makeTemporaryEnv(
{
workspaces: [`workspace`],
dependencies: {
pkg: `workspace:*`,
},
},
async ({path, run, source}) => {
await xfs.mkdirPromise(ppath.join(path, `workspace` as PortablePath));
await xfs.writeJsonPromise(ppath.join(path, `workspace/package.json` as PortablePath), {
name: `pkg`,
type: `module`,
bin: `index.mjs`,
peerDependencies: {
'no-deps': `*`,
},
});
await xfs.writeFilePromise(ppath.join(path, `workspace/index.mjs` as Filename), `import 'fs'; console.log('foo')`);
await expect(run(`install`)).resolves.toMatchObject({code: 0});
// Ensure path is virtual (ie node can't find it by default)
await expect(run(`bin`, `pkg`)).resolves.toMatchObject({
code: 0,
stdout: expect.stringContaining(`__virtual__`),
});
await expect(run(`pkg`)).resolves.toMatchObject({
code: 0,
stdout: `foo\n`,
});
},
),
);
test(
`it should enter ESM mode when target is ESM but the cwd doesn't have a pnpapi`,
makeTemporaryEnv(
{
},
async ({path, run, source}) => {
await expect(run(`dlx`, `no-deps-bins-esm`)).resolves.toMatchObject({
code: 0,
stdout: expect.stringMatching(/\n42\n$/),
});
},
),
);
test(
`it should work with dynamic imports in esm mode`,
makeTemporaryEnv(
{
type: `module`,
dependencies: {
"no-deps": `1.0.0`,
},
},
async ({path, run, source}) => {
await xfs.writeFilePromise(ppath.join(path, `index.js` as Filename), `import('no-deps').then(() => console.log(42))`);
await expect(run(`install`)).resolves.toMatchObject({code: 0});
await expect(run(`node`, `index.js`)).resolves.toMatchObject({
code: 0,
stdout: expect.stringMatching(`42\n`),
});
},
),
);
// Requires the ESM loader to be loaded but currently that enters ESM
// mode and would test the incorrect code path
test.skip(
`it should work with dynamic imports in commonjs mode`,
makeTemporaryEnv(
{
dependencies: {
"no-deps": `1.0.0`,
},
},
{
pnpEnableExperimentalEsm: true,
},
async ({path, run, source}) => {
await xfs.writeFilePromise(ppath.join(path, `index.js` as Filename), `import('no-deps').then(() => console.log(42))`);
await expect(run(`install`)).resolves.toMatchObject({code: 0});
await expect(run(`node`, `index.js`)).resolves.toMatchObject({
code: 0,
stdout: expect.stringMatching(`42\n`),
});
},
),
);
});