11cb0ef41Sopenharmony_ciimport { spawnPromisified } from '../common/index.mjs'; 21cb0ef41Sopenharmony_ciimport * as fixtures from '../common/fixtures.mjs'; 31cb0ef41Sopenharmony_ciimport assert from 'node:assert'; 41cb0ef41Sopenharmony_ciimport { execPath } from 'node:process'; 51cb0ef41Sopenharmony_ciimport { describe, it, test } from 'node:test'; 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciimport { mkdir, rm, writeFile } from 'node:fs/promises'; 81cb0ef41Sopenharmony_ciimport * as tmpdir from '../common/tmpdir.js'; 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ciimport secret from '../fixtures/experimental.json' with { type: 'json' }; 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_cidescribe('ESM: importing JSON', () => { 131cb0ef41Sopenharmony_ci it('should load JSON', () => { 141cb0ef41Sopenharmony_ci assert.strictEqual(secret.ofLife, 42); 151cb0ef41Sopenharmony_ci }); 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci it('should print an experimental warning', async () => { 181cb0ef41Sopenharmony_ci const { code, signal, stderr } = await spawnPromisified(execPath, [ 191cb0ef41Sopenharmony_ci fixtures.path('/es-modules/json-modules.mjs'), 201cb0ef41Sopenharmony_ci ]); 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci assert.match(stderr, /ExperimentalWarning: Importing JSON modules/); 231cb0ef41Sopenharmony_ci assert.strictEqual(code, 0); 241cb0ef41Sopenharmony_ci assert.strictEqual(signal, null); 251cb0ef41Sopenharmony_ci }); 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci test('should load different modules when the URL is different', async (t) => { 281cb0ef41Sopenharmony_ci const root = tmpdir.fileURL(`./test-esm-json-${Math.random()}/`); 291cb0ef41Sopenharmony_ci try { 301cb0ef41Sopenharmony_ci await mkdir(root, { recursive: true }); 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci await t.test('json', async () => { 331cb0ef41Sopenharmony_ci let i = 0; 341cb0ef41Sopenharmony_ci const url = new URL('./foo.json', root); 351cb0ef41Sopenharmony_ci await writeFile(url, JSON.stringify({ id: i++ })); 361cb0ef41Sopenharmony_ci const absoluteURL = await import(`${url}`, { 371cb0ef41Sopenharmony_ci with: { type: 'json' }, 381cb0ef41Sopenharmony_ci }); 391cb0ef41Sopenharmony_ci await writeFile(url, JSON.stringify({ id: i++ })); 401cb0ef41Sopenharmony_ci const queryString = await import(`${url}?a=2`, { 411cb0ef41Sopenharmony_ci with: { type: 'json' }, 421cb0ef41Sopenharmony_ci }); 431cb0ef41Sopenharmony_ci await writeFile(url, JSON.stringify({ id: i++ })); 441cb0ef41Sopenharmony_ci const hash = await import(`${url}#a=2`, { 451cb0ef41Sopenharmony_ci with: { type: 'json' }, 461cb0ef41Sopenharmony_ci }); 471cb0ef41Sopenharmony_ci await writeFile(url, JSON.stringify({ id: i++ })); 481cb0ef41Sopenharmony_ci const queryStringAndHash = await import(`${url}?a=2#a=2`, { 491cb0ef41Sopenharmony_ci with: { type: 'json' }, 501cb0ef41Sopenharmony_ci }); 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci assert.notDeepStrictEqual(absoluteURL, queryString); 531cb0ef41Sopenharmony_ci assert.notDeepStrictEqual(absoluteURL, hash); 541cb0ef41Sopenharmony_ci assert.notDeepStrictEqual(queryString, hash); 551cb0ef41Sopenharmony_ci assert.notDeepStrictEqual(absoluteURL, queryStringAndHash); 561cb0ef41Sopenharmony_ci assert.notDeepStrictEqual(queryString, queryStringAndHash); 571cb0ef41Sopenharmony_ci assert.notDeepStrictEqual(hash, queryStringAndHash); 581cb0ef41Sopenharmony_ci }); 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci await t.test('js', async () => { 611cb0ef41Sopenharmony_ci let i = 0; 621cb0ef41Sopenharmony_ci const url = new URL('./foo.mjs', root); 631cb0ef41Sopenharmony_ci await writeFile(url, `export default ${JSON.stringify({ id: i++ })}\n`); 641cb0ef41Sopenharmony_ci const absoluteURL = await import(`${url}`); 651cb0ef41Sopenharmony_ci await writeFile(url, `export default ${JSON.stringify({ id: i++ })}\n`); 661cb0ef41Sopenharmony_ci const queryString = await import(`${url}?a=1`); 671cb0ef41Sopenharmony_ci await writeFile(url, `export default ${JSON.stringify({ id: i++ })}\n`); 681cb0ef41Sopenharmony_ci const hash = await import(`${url}#a=1`); 691cb0ef41Sopenharmony_ci await writeFile(url, `export default ${JSON.stringify({ id: i++ })}\n`); 701cb0ef41Sopenharmony_ci const queryStringAndHash = await import(`${url}?a=1#a=1`); 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ci assert.notDeepStrictEqual(absoluteURL, queryString); 731cb0ef41Sopenharmony_ci assert.notDeepStrictEqual(absoluteURL, hash); 741cb0ef41Sopenharmony_ci assert.notDeepStrictEqual(queryString, hash); 751cb0ef41Sopenharmony_ci assert.notDeepStrictEqual(absoluteURL, queryStringAndHash); 761cb0ef41Sopenharmony_ci assert.notDeepStrictEqual(queryString, queryStringAndHash); 771cb0ef41Sopenharmony_ci assert.notDeepStrictEqual(hash, queryStringAndHash); 781cb0ef41Sopenharmony_ci }); 791cb0ef41Sopenharmony_ci } finally { 801cb0ef41Sopenharmony_ci await rm(root, { force: true, recursive: true }); 811cb0ef41Sopenharmony_ci } 821cb0ef41Sopenharmony_ci }); 831cb0ef41Sopenharmony_ci}); 84