-
Notifications
You must be signed in to change notification settings - Fork 30k
/
test-cjs-legacyMainResolve.js
177 lines (158 loc) Β· 5.23 KB
/
test-cjs-legacyMainResolve.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
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
'use strict';
// Flags: --expose-internals
require('../common');
const { describe, it } = require('node:test');
const path = require('node:path');
const assert = require('node:assert');
const { pathToFileURL } = require('node:url');
const { legacyMainResolve } = require('node:internal/modules/esm/resolve');
const fixtures = require('../common/fixtures.js');
describe('legacyMainResolve', () => {
it('should resolve using packageConfig.main', () => {
const packageJsonUrl = pathToFileURL(
path.resolve(
fixtures.path('/es-modules/legacy-main-resolver'),
'package.json'
)
);
const paths = [
['./index-js/index.js', './index-js/index.js'],
['./index-js/index', './index-js/index.js'],
['./index-json/index', './index-json/index.json'],
['./index-node/index', './index-node/index.node'],
['./index-js', './index-js/index.js'],
['./index-json', './index-json/index.json'],
['./index-node', './index-node/index.node'],
];
for (const [main, expected] of paths) {
const packageConfig = { main };
const base = path.resolve(
fixtures.path('/es-modules/legacy-main-resolver')
);
assert.strictEqual(
legacyMainResolve(packageJsonUrl, packageConfig, base).href,
pathToFileURL(path.join(base, expected)).href
);
}
});
it('should resolve using packageJsonUrl', () => {
const paths = [
['index-js', './index-js/index.js'],
['index-json', './index-json/index.json'],
['index-node', './index-node/index.node'],
];
for (const [folder, expected] of paths) {
const packageJsonUrl = pathToFileURL(
path.resolve(
fixtures.path('/es-modules/legacy-main-resolver'),
folder,
'package.json'
)
);
const packageConfig = { main: undefined };
const base = path.resolve(
fixtures.path('/es-modules/legacy-main-resolver')
);
assert.strictEqual(
legacyMainResolve(packageJsonUrl, packageConfig, base).href,
pathToFileURL(path.join(base, expected)).href
);
}
});
it('should throw when packageJsonUrl is not URL', () => {
assert.throws(
() =>
legacyMainResolve(
path.resolve(
fixtures.path('/es-modules/legacy-main-resolver/index-node'),
'package.json'
),
{},
''
),
{ code: 'ERR_INTERNAL_ASSERTION' },
);
});
it('should throw when packageConfigMain is invalid URL', () => {
assert.throws(
() =>
legacyMainResolve(
pathToFileURL(
path.resolve(
// Is invalid because this path does not point to a file
fixtures.path('/es-modules/legacy-main-resolver/index-node')
)
),
{ main: './invalid/index.js' },
''
),
{ code: 'ERR_INVALID_URL' },
);
});
it('should throw when packageJsonUrl is invalid URL', () => {
assert.throws(
() =>
legacyMainResolve(
pathToFileURL(
path.resolve(
// Is invalid because this path does not point to a file
fixtures.path('/es-modules/legacy-main-resolver/index-node')
)
),
{ main: undefined },
''
),
{ code: 'ERR_INVALID_URL' },
);
});
it('should throw when cannot resolve to a file', () => {
const packageJsonUrl = pathToFileURL(
path.resolve(
fixtures.path('/es-modules/legacy-main-resolver'),
'package.json'
)
);
assert.throws(
() => legacyMainResolve(packageJsonUrl, { main: null }, packageJsonUrl),
{ message: /index\.js/, code: 'ERR_MODULE_NOT_FOUND' },
);
});
it('should not crash when cannot resolve to a file that contains special chars', () => {
const packageJsonUrl = pathToFileURL('/c/file%20with%20percents/package.json');
assert.throws(
() => legacyMainResolve(packageJsonUrl, { main: null }, packageJsonUrl),
{ message: /index\.js/, code: 'ERR_MODULE_NOT_FOUND' },
);
});
it('should report main file on error message when not found', () => {
const packageJsonUrl = pathToFileURL(
path.resolve(
fixtures.path('/es-modules/legacy-main-resolver'),
'package.json'
)
);
assert.throws(
() => legacyMainResolve(packageJsonUrl, { main: './index.node' }, packageJsonUrl),
{ message: /index\.node/, code: 'ERR_MODULE_NOT_FOUND' },
);
});
it('should throw when cannot resolve to a file (base not defined)', () => {
const packageJsonUrl = pathToFileURL(
path.resolve(
fixtures.path('/es-modules/legacy-main-resolver'),
'package.json'
)
);
assert.throws(
() => legacyMainResolve(packageJsonUrl, { main: null }, undefined),
{ message: /"base" argument must be/, code: 'ERR_INVALID_ARG_TYPE' },
);
});
it('should interpret main as a path, not a URL', () => {
const packageJsonUrl = fixtures.fileURL('/es-modules/legacy-main-resolver/package.json');
assert.deepStrictEqual(
legacyMainResolve(packageJsonUrl, { main: '../folder%25with percentage#/' }, packageJsonUrl),
fixtures.fileURL('/es-modules/folder%25with percentage#/index.js'),
);
});
});