summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorGuy Bedford <guybedford@gmail.com>2019-12-19 16:25:52 -0500
committerShelley Vohr <shelley.vohr@gmail.com>2020-02-17 12:04:35 -0800
commitf6392e9fde507db0b9cbb5dc00e5939135a22d64 (patch)
tree5cd07e906b8c7a4e4841fc8a9574c0fec5e83e86 /test
parent8d3ffbeb55146633fee367820c39949b4e854c36 (diff)
downloadnode-new-f6392e9fde507db0b9cbb5dc00e5939135a22d64.tar.gz
esm: import.meta.resolve with nodejs: builtins
PR-URL: https://github.com/nodejs/node/pull/31032 Reviewed-By: Jan Krems <jan.krems@gmail.com> Reviewed-By: Myles Borins <myles.borins@gmail.com>
Diffstat (limited to 'test')
-rw-r--r--test/es-module/test-esm-dynamic-import.js5
-rw-r--r--test/es-module/test-esm-import-meta-resolve.mjs24
-rw-r--r--test/fixtures/es-module-loaders/example-loader.mjs4
-rw-r--r--test/fixtures/es-module-loaders/loader-unknown-builtin-module.mjs4
-rw-r--r--test/fixtures/es-module-loaders/not-found-assert-loader.mjs3
5 files changed, 32 insertions, 8 deletions
diff --git a/test/es-module/test-esm-dynamic-import.js b/test/es-module/test-esm-dynamic-import.js
index e60309fe88..e01b86eed1 100644
--- a/test/es-module/test-esm-dynamic-import.js
+++ b/test/es-module/test-esm-dynamic-import.js
@@ -55,11 +55,12 @@ function expectFsNamespace(result) {
expectFsNamespace(import('fs'));
expectFsNamespace(eval('import("fs")'));
expectFsNamespace(eval('import("fs")'));
+ expectFsNamespace(import('nodejs:fs'));
+ expectModuleError(import('nodejs:unknown'),
+ 'ERR_UNKNOWN_BUILTIN_MODULE');
expectModuleError(import('./not-an-existing-module.mjs'),
'ERR_MODULE_NOT_FOUND');
- expectModuleError(import('node:fs'),
- 'ERR_UNSUPPORTED_ESM_URL_SCHEME');
expectModuleError(import('http://example.com/foo.js'),
'ERR_UNSUPPORTED_ESM_URL_SCHEME');
})();
diff --git a/test/es-module/test-esm-import-meta-resolve.mjs b/test/es-module/test-esm-import-meta-resolve.mjs
new file mode 100644
index 0000000000..faf9320a82
--- /dev/null
+++ b/test/es-module/test-esm-import-meta-resolve.mjs
@@ -0,0 +1,24 @@
+// Flags: --experimental-import-meta-resolve
+import '../common/index.mjs';
+import assert from 'assert';
+
+const dirname = import.meta.url.slice(0, import.meta.url.lastIndexOf('/') + 1);
+const fixtures = dirname.slice(0, dirname.lastIndexOf('/', dirname.length - 2) +
+ 1) + 'fixtures/';
+
+(async () => {
+ assert.strictEqual(await import.meta.resolve('./test-esm-import-meta.mjs'),
+ dirname + 'test-esm-import-meta.mjs');
+ try {
+ await import.meta.resolve('./notfound.mjs');
+ assert.fail();
+ } catch (e) {
+ assert.strictEqual(e.code, 'ERR_MODULE_NOT_FOUND');
+ }
+ assert.strictEqual(
+ await import.meta.resolve('../fixtures/empty-with-bom.txt'),
+ fixtures + 'empty-with-bom.txt');
+ assert.strictEqual(await import.meta.resolve('../fixtures/'), fixtures);
+ assert.strictEqual(await import.meta.resolve('baz/', fixtures),
+ fixtures + 'node_modules/baz/');
+})();
diff --git a/test/fixtures/es-module-loaders/example-loader.mjs b/test/fixtures/es-module-loaders/example-loader.mjs
index 70f9f28f08..1ed18bda51 100644
--- a/test/fixtures/es-module-loaders/example-loader.mjs
+++ b/test/fixtures/es-module-loaders/example-loader.mjs
@@ -11,7 +11,7 @@ baseURL.pathname = process.cwd() + '/';
export function resolve(specifier, { parentURL = baseURL }, defaultResolve) {
if (builtinModules.includes(specifier)) {
return {
- url: specifier
+ url: 'nodejs:' + specifier
};
}
if (/^\.{1,2}[/]/.test(specifier) !== true && !specifier.startsWith('file:')) {
@@ -27,7 +27,7 @@ export function resolve(specifier, { parentURL = baseURL }, defaultResolve) {
}
export function getFormat(url, context, defaultGetFormat) {
- if (builtinModules.includes(url)) {
+ if (url.startsWith('nodejs:') && builtinModules.includes(url.slice(7))) {
return {
format: 'builtin'
};
diff --git a/test/fixtures/es-module-loaders/loader-unknown-builtin-module.mjs b/test/fixtures/es-module-loaders/loader-unknown-builtin-module.mjs
index 1a48231966..e976343e47 100644
--- a/test/fixtures/es-module-loaders/loader-unknown-builtin-module.mjs
+++ b/test/fixtures/es-module-loaders/loader-unknown-builtin-module.mjs
@@ -1,14 +1,14 @@
export async function resolve(specifier, { parentURL }, defaultResolve) {
if (specifier === 'unknown-builtin-module') {
return {
- url: 'unknown-builtin-module'
+ url: 'nodejs:unknown-builtin-module'
};
}
return defaultResolve(specifier, {parentURL}, defaultResolve);
}
export async function getFormat(url, context, defaultGetFormat) {
- if (url === 'unknown-builtin-module') {
+ if (url === 'nodejs:unknown-builtin-module') {
return {
format: 'builtin'
};
diff --git a/test/fixtures/es-module-loaders/not-found-assert-loader.mjs b/test/fixtures/es-module-loaders/not-found-assert-loader.mjs
index 7b1d176e45..2130bad5f5 100644
--- a/test/fixtures/es-module-loaders/not-found-assert-loader.mjs
+++ b/test/fixtures/es-module-loaders/not-found-assert-loader.mjs
@@ -14,8 +14,7 @@ export async function resolve(specifier, { parentURL }, defaultResolve) {
catch (e) {
assert.strictEqual(e.code, 'ERR_MODULE_NOT_FOUND');
return {
- format: 'builtin',
- url: 'fs'
+ url: 'nodejs:fs'
};
}
assert.fail(`Module resolution for ${specifier} should be throw ERR_MODULE_NOT_FOUND`);