/
pnpapi-v1.js
100 lines (82 loc) · 3.15 KB
/
pnpapi-v1.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
94
95
96
97
98
99
100
const {normalize} = require('path');
const {fs: {writeFile, writeJson}} = require('pkg-tests-core');
module.exports = makeTemporaryEnv => {
describe(`Plug'n'Play API (v1)`, () => {
test(
`it should expose VERSIONS`,
makeTemporaryEnv({}, {plugNPlay: true}, async ({path, run, source}) => {
await run(`install`);
await expect(source(`require('pnpapi').VERSIONS`)).resolves.toMatchObject({std: 1});
}),
);
test(
`it should expose resolveToUnqualified`,
makeTemporaryEnv({}, {plugNPlay: true}, async ({path, run, source}) => {
await run(`install`);
await expect(source(`typeof require('pnpapi').resolveToUnqualified`)).resolves.toEqual(`function`);
}),
);
test(
`it should expose resolveToUnqualified`,
makeTemporaryEnv({}, {plugNPlay: true}, async ({path, run, source}) => {
await run(`install`);
await expect(source(`typeof require('pnpapi').resolveUnqualified`)).resolves.toEqual(`function`);
}),
);
test(
`it should expose resolveToUnqualified`,
makeTemporaryEnv({}, {plugNPlay: true}, async ({path, run, source}) => {
await run(`install`);
await expect(source(`typeof require('pnpapi').resolveRequest`)).resolves.toEqual(`function`);
}),
);
describe(`resolveRequest`, () => {
test(
`it should return the path to the PnP file for when 'pnpapi' is requested`,
makeTemporaryEnv({}, {plugNPlay: true}, async ({path, run, source}) => {
await run(`install`);
await expect(source(`require('pnpapi').resolveRequest('pnpapi', '${path}/')`)).resolves.toEqual(
normalize(`${path}/.pnp.js`),
);
}),
);
test(
`it should return null for builtins`,
makeTemporaryEnv({}, {plugNPlay: true}, async ({path, run, source}) => {
await run(`install`);
await expect(source(`require('pnpapi').resolveRequest('fs', '${path}/')`)).resolves.toEqual(null);
}),
);
test(
`it should support the 'considerBuiltins' option`,
makeTemporaryEnv(
{
dependencies: {[`fs`]: `link:./fs`},
},
{plugNPlay: true},
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', '${path}/', {considerBuiltins: false})`),
).resolves.toEqual(`${path}/fs/index.js`);
},
),
);
test(
`it should support the 'extensions' option`,
makeTemporaryEnv({}, {plugNPlay: true}, async ({path, run, source}) => {
await writeFile(`${path}/foo.bar`, `hello world`);
await run(`install`);
await expect(
source(`require('pnpapi').resolveRequest('./foo', '${path}/', {extensions: ['.bar']})`),
).resolves.toEqual(`${path}/foo.bar`);
}),
);
});
});
};