forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-esm-import-meta-resolve.mjs
72 lines (66 loc) · 2.46 KB
/
test-esm-import-meta-resolve.mjs
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
// Flags: --experimental-import-meta-resolve
import '../common/index.mjs';
import assert from 'assert';
import { spawn } from 'child_process';
import { execPath } from 'process';
const dirname = import.meta.url.slice(0, import.meta.url.lastIndexOf('/') + 1);
const fixtures = dirname.slice(0, dirname.lastIndexOf('/', dirname.length - 2) + 1) + 'fixtures/';
assert.strictEqual(import.meta.resolve('./test-esm-import-meta.mjs'),
dirname + 'test-esm-import-meta.mjs');
try {
import.meta.resolve('./notfound.mjs');
assert.fail();
} catch (e) {
assert.strictEqual(e.code, 'ERR_MODULE_NOT_FOUND');
}
assert.strictEqual(
import.meta.resolve('../fixtures/empty-with-bom.txt'),
fixtures + 'empty-with-bom.txt');
assert.strictEqual(import.meta.resolve('../fixtures/'), fixtures);
assert.strictEqual(
import.meta.resolve('../fixtures/', import.meta.url),
fixtures);
assert.strictEqual(
import.meta.resolve('../fixtures/', new URL(import.meta.url)),
fixtures);
[[], {}, Symbol(), 0, 1, 1n, 1.1, () => {}, true, false].map((arg) =>
assert.throws(() => import.meta.resolve('../fixtures/', arg), {
code: 'ERR_INVALID_ARG_TYPE',
})
);
assert.strictEqual(import.meta.resolve('http://some-absolute/url'), 'http://some-absolute/url');
assert.strictEqual(import.meta.resolve('some://weird/protocol'), 'some://weird/protocol');
assert.strictEqual(import.meta.resolve('baz/', fixtures),
fixtures + 'node_modules/baz/');
{
const cp = spawn(execPath, [
'--experimental-import-meta-resolve',
'--input-type=module',
'--eval', 'console.log(typeof import.meta.resolve)',
]);
assert.match((await cp.stdout.toArray()).toString(), /^function\r?\n$/);
}
{
const cp = spawn(execPath, [
'--experimental-import-meta-resolve',
'--input-type=module',
]);
cp.stdin.end('console.log(typeof import.meta.resolve)');
assert.match((await cp.stdout.toArray()).toString(), /^function\r?\n$/);
}
{
const cp = spawn(execPath, [
'--experimental-import-meta-resolve',
'--input-type=module',
'--eval', 'import "data:text/javascript,console.log(import.meta.resolve(%22node:os%22))"',
]);
assert.match((await cp.stdout.toArray()).toString(), /^node:os\r?\n$/);
}
{
const cp = spawn(execPath, [
'--experimental-import-meta-resolve',
'--input-type=module',
]);
cp.stdin.end('import "data:text/javascript,console.log(import.meta.resolve(%22node:os%22))"');
assert.match((await cp.stdout.toArray()).toString(), /^node:os\r?\n$/);
}