1// Flags: --experimental-import-meta-resolve 2import '../common/index.mjs'; 3import assert from 'assert'; 4import { spawn } from 'child_process'; 5import { execPath } from 'process'; 6 7const dirname = import.meta.url.slice(0, import.meta.url.lastIndexOf('/') + 1); 8const fixtures = dirname.slice(0, dirname.lastIndexOf('/', dirname.length - 2) + 1) + 'fixtures/'; 9 10assert.strictEqual(import.meta.resolve('./test-esm-import-meta.mjs'), 11 dirname + 'test-esm-import-meta.mjs'); 12assert.strictEqual(import.meta.resolve('./notfound.mjs'), new URL('./notfound.mjs', import.meta.url).href); 13assert.strictEqual(import.meta.resolve('./asset'), new URL('./asset', import.meta.url).href); 14try { 15 import.meta.resolve('does-not-exist'); 16 assert.fail(); 17} catch (e) { 18 assert.strictEqual(e.code, 'ERR_MODULE_NOT_FOUND'); 19} 20assert.strictEqual( 21 import.meta.resolve('../fixtures/empty-with-bom.txt'), 22 fixtures + 'empty-with-bom.txt'); 23assert.strictEqual(import.meta.resolve('../fixtures/'), fixtures); 24assert.strictEqual( 25 import.meta.resolve('../fixtures/', import.meta.url), 26 fixtures); 27assert.strictEqual( 28 import.meta.resolve('../fixtures/', new URL(import.meta.url)), 29 fixtures); 30[[], {}, Symbol(), 0, 1, 1n, 1.1, () => {}, true, false].map((arg) => 31 assert.throws(() => import.meta.resolve('../fixtures/', arg), { 32 code: 'ERR_INVALID_ARG_TYPE', 33 }) 34); 35assert.strictEqual(import.meta.resolve('baz/', fixtures), 36 fixtures + 'node_modules/baz/'); 37 38{ 39 const cp = spawn(execPath, [ 40 '--input-type=module', 41 '--eval', 'console.log(typeof import.meta.resolve)', 42 ]); 43 assert.match((await cp.stdout.toArray()).toString(), /^function\r?\n$/); 44} 45 46{ 47 const cp = spawn(execPath, [ 48 '--input-type=module', 49 ]); 50 cp.stdin.end('console.log(typeof import.meta.resolve)'); 51 assert.match((await cp.stdout.toArray()).toString(), /^function\r?\n$/); 52} 53 54{ 55 const cp = spawn(execPath, [ 56 '--input-type=module', 57 '--eval', 'import "data:text/javascript,console.log(import.meta.resolve(%22node:os%22))"', 58 ]); 59 assert.match((await cp.stdout.toArray()).toString(), /^node:os\r?\n$/); 60} 61 62{ 63 const cp = spawn(execPath, [ 64 '--input-type=module', 65 ]); 66 cp.stdin.end('import "data:text/javascript,console.log(import.meta.resolve(%22node:os%22))"'); 67 assert.match((await cp.stdout.toArray()).toString(), /^node:os\r?\n$/); 68} 69