1import { spawnPromisified } from '../common/index.mjs'; 2import * as fixtures from '../common/fixtures.mjs'; 3import assert from 'node:assert'; 4import { execPath } from 'node:process'; 5import { describe, it } from 'node:test'; 6 7describe('default resolver', () => { 8 // In these tests `byop` is an acronym for "bring your own protocol", and is the 9 // protocol our byop-dummy-loader.mjs can load 10 it('should accept foreign schemas without exception (e.g. byop://something/or-other)', async () => { 11 const { code, stdout, stderr } = await spawnPromisified(execPath, [ 12 '--no-warnings', 13 '--experimental-loader', 14 fixtures.fileURL('/es-module-loaders/byop-dummy-loader.mjs'), 15 '--input-type=module', 16 '--eval', 17 "import 'byop://1/index.mjs'", 18 ]); 19 assert.strictEqual(code, 0); 20 assert.strictEqual(stdout.trim(), 'index.mjs!'); 21 assert.strictEqual(stderr, ''); 22 }); 23 24 it('should resolve foreign schemas by doing regular url absolutization', async () => { 25 const { code, stdout, stderr } = await spawnPromisified(execPath, [ 26 '--no-warnings', 27 '--experimental-loader', 28 fixtures.fileURL('/es-module-loaders/byop-dummy-loader.mjs'), 29 '--input-type=module', 30 '--eval', 31 "import 'byop://1/index2.mjs'", 32 ]); 33 assert.strictEqual(code, 0); 34 assert.strictEqual(stdout.trim(), '42'); 35 assert.strictEqual(stderr, ''); 36 }); 37 38 // In this test, `byoe` is an acronym for "bring your own extension" 39 it('should accept foreign extensions without exception (e.g. ..//something.byoe)', async () => { 40 const { code, stdout, stderr } = await spawnPromisified(execPath, [ 41 '--no-warnings', 42 '--experimental-loader', 43 fixtures.fileURL('/es-module-loaders/byop-dummy-loader.mjs'), 44 '--input-type=module', 45 '--eval', 46 "import 'byop://1/index.byoe'", 47 ]); 48 assert.strictEqual(code, 0); 49 assert.strictEqual(stdout.trim(), 'index.byoe!'); 50 assert.strictEqual(stderr, ''); 51 }); 52 53 it('should identify the parent module of an invalid URL host in import specifier', async () => { 54 if (process.platform === 'win32') return; 55 56 const { code, stderr } = await spawnPromisified(execPath, [ 57 '--no-warnings', 58 fixtures.path('es-modules', 'invalid-posix-host.mjs'), 59 ]); 60 61 assert.match(stderr, /ERR_INVALID_FILE_URL_HOST/); 62 assert.match(stderr, /file:\/\/hmm\.js/); 63 assert.match(stderr, /invalid-posix-host\.mjs/); 64 assert.strictEqual(code, 1); 65 }); 66}); 67