node/test/es-module/test-esm-import-meta-main.mjs
Joe bbc059378b
esm: implement import.meta.main
Boolean value to check if an ES Module is the entrypoint of the
current process.

Implements: #57226

Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/57804
Fixes: https://github.com/nodejs/node/issues/57226
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
2025-05-26 22:31:54 +00:00

26 lines
781 B
JavaScript

import '../common/index.mjs';
import assert from 'node:assert/strict';
import { Worker } from 'node:worker_threads';
function get_environment() {
if (process.env.HAS_STARTED_WORKER) return 'in worker thread started by ES Module';
return 'in ES Module';
}
assert.strictEqual(
import.meta.main,
true,
`\`import.meta.main\` at top-level module ${get_environment()} should evaluate \`true\``
);
const { isMain: importedModuleIsMain } = await import('../fixtures/es-modules/import-meta-main.mjs');
assert.strictEqual(
importedModuleIsMain,
false,
`\`import.meta.main\` at dynamically imported module ${get_environment()} should evaluate \`false\``
);
if (!process.env.HAS_STARTED_WORKER) {
process.env.HAS_STARTED_WORKER = 1;
new Worker(import.meta.filename);
}