diff --git a/test/es-module/test-esm-import-flag.js b/test/es-module/test-esm-import-flag.js new file mode 100644 index 00000000000000..5de61d53adf990 --- /dev/null +++ b/test/es-module/test-esm-import-flag.js @@ -0,0 +1,75 @@ +'use strict'; + +const fixtures = require('../common/fixtures'); +const assert = require('assert'); +const { spawnSync } = require('child_process'); + +{ + // known limitation: + // imports should not be imported when evaluating text + const { status, stderr, stdout } = spawnSync( + process.execPath, + [ + '--experimental-import', + fixtures.path('es-modules', 'mjs-file.mjs'), + '-e', 'console.log("log")' + ], + { encoding: 'utf8' }, + ); + + assert.strictEqual(status, 0); + assert.strictEqual(stderr, ''); + assert.strictEqual(stdout, 'log\n'); +} + +{ + const { status, stderr, stdout } = spawnSync( + process.execPath, + [ + '--experimental-import', + fixtures.path('es-modules', 'mjs-file.mjs'), + fixtures.path('es-modules', 'cjs-file.cjs') + ], + { encoding: 'utf8' }, + ); + + assert.strictEqual(status, 0); + assert.strictEqual(stderr, ''); + assert.strictEqual(stdout, '.mjs file\n.cjs file\n'); +} + +{ + const { status, stderr, stdout } = spawnSync( + process.execPath, + [ + '--experimental-import', + fixtures.path('es-modules', 'mjs-file.mjs'), + fixtures.path('es-modules', 'mjs-file.mjs') + ], + { encoding: 'utf8' }, + ); + + assert.strictEqual(status, 0); + assert.strictEqual(stderr, ''); + assert.strictEqual(stdout, '.mjs file\n.mjs file\n'); +} + +{ + let start = Date.now(); + const { status, stderr, stdout } = spawnSync( + process.execPath, + [ + '--experimental-import', + fixtures.path('es-modules', 'esm-top-level-await.mjs'), + fixtures.path('es-modules', 'print_imported.mjs') + ], + { encoding: 'utf8' }, + ); + + assert.strictEqual(status, 0); + assert.strictEqual(stderr, ''); + const output = JSON.parse(stdout); + assert.ok(output.time >= start); + assert.ok(output.time_async > start); + assert.ok(output.time_async_delayed >= start + 100); +} \ No newline at end of file diff --git a/test/fixtures/es-modules/esm-top-level-await.mjs b/test/fixtures/es-modules/esm-top-level-await.mjs new file mode 100644 index 00000000000000..1b974b4cb5f08a --- /dev/null +++ b/test/fixtures/es-modules/esm-top-level-await.mjs @@ -0,0 +1,18 @@ +import { setTimeout } from 'timers/promises'; + +export const time = Date.now(); + +export const time_async = await new Promise((resolve) => { + process.nextTick(() => resolve(Date.now())) +}) + +export const time_async_delayed = await new Promise(async (resolve) => { + await setTimeout(100) + resolve(Date.now()); +}); + +globalThis.imported = { + time, + time_async, + time_async_delayed +}; \ No newline at end of file diff --git a/test/fixtures/es-modules/print_imported.mjs b/test/fixtures/es-modules/print_imported.mjs new file mode 100644 index 00000000000000..e2bcf12910048d --- /dev/null +++ b/test/fixtures/es-modules/print_imported.mjs @@ -0,0 +1 @@ +process.stdout.write(JSON.stringify(imported)); \ No newline at end of file