Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions test/es-module/test-cjs-defer-static-import-eval.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Flags: --js-defer-import-eval

// Test that uses import.defer for a CJS module. It ensures that:
// 1. the module is imported successfully
// 2. it's evaluated synchronously, regardless of the `defer` modifier.

@joyeecheung joyeecheung Aug 5, 2026

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.

Suggested change
// 2. it's evaluated synchronously, regardless of the `defer` modifier.
// 2. it's evaluated synchronously, regardless of the `defer` modifier.
// 3. Evaluation of the imported module is deferred until first namespace access.


import '../common/index.mjs';
import * as assert from 'assert';

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

globalThis.eval_list = [];

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.

Suggested change
globalThis.eval_list = [];
// At this point, the deferred module should not yet be evaluated. Initialize the
// eval_list which will be populated only when the module is evaluated for the
// first time triggered by namespace access below.
globalThis.eval_list = [];


// Check that the exported properties are accessible and have the
Comment thread
joyeecheung marked this conversation as resolved.
// expected values.
Comment on lines +15 to +16

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.

Suggested change
// Check that the exported properties are accessible and have the
// expected values.

This is a bit self-evident.

assert.strictEqual(imported.foo, 42);
assert.strictEqual(imported.identifier, 'package-type-commonjs');

// Check that the module has been evaluated at this point,
// also that it's not evaluated more than once.
assert.deepStrictEqual(['defer-1'], globalThis.eval_list);

// Clean-up
delete globalThis.eval_list;
17 changes: 17 additions & 0 deletions test/fixtures/es-modules/module-cjs-deferred-eval.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// This fixture is imported as a module
// in test/es-module/test-cjs-defer-static-import-eval.mjs
// to ensure a CommonJS module imported with `import defer`
// is only executed once.
const assert = require('assert');

const identifier = 'package-type-commonjs';

module.exports.foo = 42;
module.exports.identifier = identifier;

// The `eval_list` is initialised by the importing module,
// so by the time the fixture is executed, `eval_list` should
// already be initialised.
assert.deepEqual(globalThis.eval_list, []);

globalThis.eval_list.push('defer-1');
Loading