Skip to content

test: add a simple test for import defer of a CJS module - #64694

Open
MayaLekova wants to merge 1 commit into
nodejs:mainfrom
MayaLekova:import-defer-04-cjs
Open

test: add a simple test for import defer of a CJS module#64694
MayaLekova wants to merge 1 commit into
nodejs:mainfrom
MayaLekova:import-defer-04-cjs

Conversation

@MayaLekova

Copy link
Copy Markdown
Contributor

test: add a simple test for import defer of a CJS module

This tests imports a CommonJS modules with the defer modifier.
It ensures that the imported module is not evaluated before
accessing properties form its exports.

@nodejs-github-bot nodejs-github-bot added esm Issues and PRs related to the ECMAScript Modules implementation. needs-ci PRs that need a full CI run. test Issues and PRs related to the tests. labels Jul 23, 2026
@MayaLekova

Copy link
Copy Markdown
Contributor Author

@nicolo-ribaudo and @joyeecheung please take a look, thanks!

@MayaLekova
MayaLekova force-pushed the import-defer-04-cjs branch from de9e5ac to 2d51c34 Compare July 23, 2026 10:24
Comment thread test/es-module/test-cjs-defer-static-import-eval.mjs Outdated
Comment thread test/es-module/test-cjs-defer-static-import-eval.mjs Outdated
Comment thread test/es-module/test-cjs-defer-static-import-eval.mjs Outdated
@codecov

codecov Bot commented Jul 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.15%. Comparing base (4bec191) to head (9849023).
⚠️ Report is 90 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #64694      +/-   ##
==========================================
+ Coverage   90.13%   90.15%   +0.02%     
==========================================
  Files         741      746       +5     
  Lines      242106   242660     +554     
  Branches    45551    45723     +172     
==========================================
+ Hits       218211   218777     +566     
+ Misses      15430    15390      -40     
- Partials     8465     8493      +28     

see 105 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread test/fixtures/es-modules/module-cjs-deferred-eval.js Outdated
Comment on lines +10 to +14
globalThis.eval_list = [];

// Import the CJS module with the `defer` modifier.
// import defer * as imported from '../fixtures/es-modules/package-type-commonjs/index.js';
import defer * as imported from '../fixtures/es-modules/module-cjs-deferred-eval.js';

@nicolo-ribaudo nicolo-ribaudo Jul 24, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to swap these two statements, so that when reading somebody does not mistakenly assume that globalThis.eval_list = [] would be evaluated first (imports are hoisted and executed before code in the file).

This test right now would pass regardless of whether import defer is eager or deferred. If it's eager it passes because:

  • after the eager ../fixtures/es-modules/module-cjs-deferred-eval.js evaluation, this file sets globalThis.eval_list = [];. Then it asserts that its length is 0, which is obviously true because we just set it to an empty array.
  • further down, in assert.partialDeepStrictEqual(['defer-1'], globalThis.eval_list);, we are checking that ['defer-1'] contains all the elements of globalThis.eval_list. If globalThis.eval_list is empty, that's tautologically true.

I have not run this test, but I assume the reason in test/fixtures/es-modules/module-cjs-deferred-eval.js you had to add if (!globalThis.eval_list) { globalThis.eval_list = []; } is because the CJS module is indeed not being deferred, so it's running before that this file defines globalThis.eval_list = [].

Maybe better, either one of this:

  • move the globalThis.eval_list = [] initialization to a separate file that is imported at the beginning of this one, before the other imports, so that we know it runs first
  • use a boolean instead of an array, and then we don't need to pre-initialize it.

@MayaLekova MayaLekova Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation! I was trying to finish the test in a rush and didn't test properly.
The imported module doesn't need to initialize the array at all, so I just moved it to the main module. Having eval_list as an array makes it easier to check the imported module is executed only once (using deepStrictEqual as Joyee suggested), so I'm leaving it as an array.

This tests imports a CommonJS modules with the `defer` modifier.
It ensures that the imported module is not evaluated before
accessing properties from its exports.

Signed-off-by: Maya Lekova <maya@igalia.com>
@MayaLekova
MayaLekova force-pushed the import-defer-04-cjs branch from 2d51c34 to 9849023 Compare July 29, 2026 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

esm Issues and PRs related to the ECMAScript Modules implementation. needs-ci PRs that need a full CI run. test Issues and PRs related to the tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants