Skip to content

test: add smoke tests for defer importing synthetic modules - #65537

Open
MayaLekova wants to merge 1 commit into
nodejs:mainfrom
MayaLekova:import-defer-05-synthetic
Open

MayaLekova wants to merge 1 commit into
nodejs:mainfrom
MayaLekova:import-defer-05-synthetic

Conversation

@MayaLekova

Copy link
Copy Markdown
Contributor

test: add smoke tests for defer importing synthetic modules

The tests added ensure that Node.js doesn't crash or produce incorrect results when importing synthetic modules (i.e. JSON, text or builtin modules) with the defer modifier.

Signed-off-by: Maya Lekova maya@igalia.com

@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 Node.js core tests and test infrastructure. labels Aug 25, 2026
@MayaLekova
MayaLekova force-pushed the import-defer-05-synthetic branch 2 times, most recently from 03ccd50 to 414cd49 Compare August 25, 2026 11:23
@MayaLekova
MayaLekova marked this pull request as ready for review August 25, 2026 11:24
@MayaLekova

Copy link
Copy Markdown
Contributor Author

@joyeecheung and @nicolo-ribaudo could you take a look please? Just a few smoke tests for synthetic modules.

@codecov

codecov Bot commented Aug 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.24%. Comparing base (46a7dbd) to head (46df383).
⚠️ Report is 443 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #65537      +/-   ##
==========================================
+ Coverage   90.21%   90.24%   +0.03%     
==========================================
  Files         751      789      +38     
  Lines      253550   270476   +16926     
  Branches    47813    51751    +3938     
==========================================
+ Hits       228733   244092   +15359     
- Misses      16076    16868     +792     
- Partials     8741     9516     +775     

see 287 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.

@nicolo-ribaudo nicolo-ribaudo left a comment

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.

The tested behavior looks correct to me.

For the node:fs, is there any way to test when evaluation happens? Maybe if one monkey-patches some built-ins before evaluating node:fs then it does something different? (I hope the answer is "no it's not possible")

@MayaLekova

Copy link
Copy Markdown
Contributor Author

is there any way to test when evaluation happens?
I thought about it, couldn't think of a straight-forward way at first. Monkey-patching might be a reasonable way, I'll give it another try.

If the builtin was to write something to the global namespace, that would make it easier. But I guess they don't do so, in order no to pollute the global namespace. Also there might be another builtin module that writes something on globalThis.

@MayaLekova
MayaLekova force-pushed the import-defer-05-synthetic branch 2 times, most recently from 0302cad to 7f7855e Compare August 25, 2026 17:48
@MayaLekova

MayaLekova commented Aug 31, 2026

Copy link
Copy Markdown
Contributor Author

Suggestion for now - I can land those tests as they are currently, and then as a follow up:

  • implement a new native function in V8, e.g. %GetModuleStatus(module_object) that returns the evaluation status as a number or string; this might be useful for other tests too.
  • expose the ModuleStatus property (also proposed in this design doc concerning Chrome DevTools) as constants to JavaScript, if GetModuleStatus returns a number.
  • use the native function in the test with the builtin module.

Is there another mechanism in Node.js to expose internal details from C++ to JS?

@joyeecheung

joyeecheung commented Sep 9, 2026

Copy link
Copy Markdown
Member

Can you import a module that's not in the snapshot (something like http should do), and use process.moduleLoadList to check whether the evaluation is deferred?

@joyeecheung

joyeecheung commented Sep 9, 2026

Copy link
Copy Markdown
Member

Is there another mechanism in Node.js to expose internal details from C++ to JS?

You can simply get to the ModuleWrap of the said module and check wrap.getStatus(). The wraps should be reachable through the module load cache.

@MayaLekova

Copy link
Copy Markdown
Contributor Author

Can you import a module that's not in the snapshot (something like http should do), and use process.moduleLoadList to check whether the evaluation is deferred?

Wouldn't this work only if --node-builtin-modules-path during building Node.js? In case builtins are baked into the binary, process.moduleLoadList contains 'NativeModule internal/http' and 'NativeModule http' already at the beginning of the script.

@MayaLekova
MayaLekova force-pushed the import-defer-05-synthetic branch from 7f7855e to c6bb77a Compare September 14, 2026 14:03
@MayaLekova

MayaLekova commented Sep 14, 2026

Copy link
Copy Markdown
Contributor Author

Is there another mechanism in Node.js to expose internal details from C++ to JS?

You can simply get to the ModuleWrap of the said module and check wrap.getStatus(). The wraps should be reachable through the module load cache.

Okay, I added a new version of the test that uses some internal machinery to obtain the ModuleWrap from the internal module. Still, the checks are not very interesting, as it seems that the module status is "evaluated" right from the beginning of the test.

@joyeecheung Please take another look, thanks!

Comment on lines +15 to +18
// Load the http builtin module and check that it is evaluated.
let builtin = helpers.default.loadBuiltinModule('http');
let wrap = builtin.getESMFacade();
assert.strictEqual(wrap.getStatus(), kEvaluated);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this needs to first check that: before you load the builtin, it's not in the moduleLoad list. Afterwards, it is. And the difference of whether import defer is implemented is whether it's there right away or only after you have accessed anything from http.

@MayaLekova MayaLekova Sep 14, 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.

@joyeecheung The thing is, it's already in the module load list, even before accessing the module. E.g. this code

let modules = process.moduleLoadList.filter((item) => item.endsWith('http'))
console.log(modules);

prints [ 'NativeModule internal/http', 'NativeModule http' ]. Do you know why it's loaded eagerly?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You mean in the test or standalone? If you run this snippet alone it's pretty both on main branch and in current releases. If you have code that imports http then it surely will print that because ESM import is hoisted and dependencies are loaded before the current module is executed, even if you put the import statement below the log.

The tests added ensure that Node.js doesn't crash or produce
incorrect results when importing synthetic modules (i.e. JSON,
text or builtin modules) with the `defer` modifier.

Signed-off-by: Maya Lekova <maya@igalia.com>
@MayaLekova
MayaLekova force-pushed the import-defer-05-synthetic branch from c6bb77a to 46df383 Compare September 15, 2026 12:22

// Test that uses import.defer for a builtin module. Currently
// defer importing of a synthetic module should be a no-op
// in Node.js, so the test is mostly a smoke test that Node

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.

Suggested change
// in Node.js, so the test is mostly a smoke test that Node
// in Node.js as they are born pre-evaluated, so the test is mostly a smoke test that Node

with { type: 'json' };

// Check that the imported object has the expected key/value.
assert.strictEqual(imported_json.default.rocko, 'artischocko');

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.

Can we also test that imported_json.default === same_import_but_without_defer.default?

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 Node.js core tests and test infrastructure.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants