From af6b72aafd4d57461eb0e22aa93425c11e84fc0d Mon Sep 17 00:00:00 2001 From: MayaLekova Date: Mon, 29 Jun 2026 16:55:44 +0300 Subject: [PATCH] module: add tests for two cases of eager imports: - when the defer imported module contains top-level await - when the defer imported module is eagerly imported from another module Refs: https://github.com/tc39/proposal-defer-import-eval Signed-off-by: Maya Lekova --- .../test-defer-import-eval-with-tla.mjs | 24 +++++++++++++++++++ .../test-defer-import-with-regular-import.mjs | 23 ++++++++++++++++++ .../es-modules/dep-module-top-level-await.mjs | 11 +++++++++ 3 files changed, 58 insertions(+) create mode 100644 test/es-module/test-defer-import-eval-with-tla.mjs create mode 100644 test/es-module/test-defer-import-with-regular-import.mjs create mode 100644 test/fixtures/es-modules/dep-module-top-level-await.mjs diff --git a/test/es-module/test-defer-import-eval-with-tla.mjs b/test/es-module/test-defer-import-eval-with-tla.mjs new file mode 100644 index 00000000000000..db546ccbb092ec --- /dev/null +++ b/test/es-module/test-defer-import-eval-with-tla.mjs @@ -0,0 +1,24 @@ +// Flags: --js-defer-import-eval + +// Tests that defer import of a module with top-level await +// eagerly evaluates the imported module. + +import '../common/index.mjs'; +import * as assert from 'assert'; + +// The importing module will only execute once its dependency containing +// top-level await has its promises resolved, as per +// https://github.com/tc39/proposal-top-level-await#what-exactly-is-blocked-by-a-top-level-await +if (!globalThis.eval_list) { + globalThis.eval_list = []; +} + +import defer * as ns from '../fixtures/es-modules/dep-module-top-level-await.mjs'; + +// Check that the module has already been evaluated. +assert.strictEqual(globalThis.eval_list.length, 1); +assert.strictEqual(ns.foo, 42); +assert.partialDeepStrictEqual(['defer-tla-1'], globalThis.eval_list); + +// Clean-up +delete globalThis.eval_list; diff --git a/test/es-module/test-defer-import-with-regular-import.mjs b/test/es-module/test-defer-import-with-regular-import.mjs new file mode 100644 index 00000000000000..6d3f765ae3008c --- /dev/null +++ b/test/es-module/test-defer-import-with-regular-import.mjs @@ -0,0 +1,23 @@ +// Flags: --js-defer-import-eval + +// Tests that defer import of a module that is also eagerly imported +// from another dependency gets executed synchronously, and gets +// executed exactly once. + +import '../common/index.mjs'; +import * as assert from 'assert'; + +if (!globalThis.eval_list) { + globalThis.eval_list = []; +} + +import * as non_deferred from '../fixtures/es-modules/module-with-module-tree.mjs'; +import defer * as deferred from '../fixtures/es-modules/module-deferred-eval.mjs'; + +// Check that the module has been evaluated exactly once. +assert.strictEqual(globalThis.eval_list.length, 1); +assert.strictEqual(deferred.foo, 42); +assert.partialDeepStrictEqual(['defer-1'], globalThis.eval_list); + +// Clean-up +delete globalThis.eval_list; diff --git a/test/fixtures/es-modules/dep-module-top-level-await.mjs b/test/fixtures/es-modules/dep-module-top-level-await.mjs new file mode 100644 index 00000000000000..4ef3c827350fca --- /dev/null +++ b/test/fixtures/es-modules/dep-module-top-level-await.mjs @@ -0,0 +1,11 @@ +import { setTimeout } from 'node:timers/promises'; + +if (!globalThis.eval_list) { + globalThis.eval_list = []; +} +globalThis.eval_list.push('defer-tla-1'); + +export const foo = 42; + +await setTimeout(1); +console.log('executed');