|
| 1 | +// Flags: --js-defer-import-eval |
| 2 | + |
| 3 | +// Test that uses import.defer for a CJS module. It ensures that: |
| 4 | +// 1. the module is imported successfully; |
| 5 | +// 2. it's evaluated synchronously, regardless of the `defer` modifier; |
| 6 | +// 3. Evaluation of the imported module is deferred |
| 7 | +// until first namespace access. |
| 8 | + |
| 9 | +import '../common/index.mjs'; |
| 10 | +import * as assert from 'assert'; |
| 11 | + |
| 12 | +// Import the CJS module with the `defer` modifier. |
| 13 | +import defer * as imported from '../fixtures/es-modules/module-cjs-deferred-eval.js'; |
| 14 | + |
| 15 | +// At this point, the deferred module should not yet be evaluated. Initialize |
| 16 | +// the `eval_list`, which will be populated only when the module is evaluated |
| 17 | +// for the first time, triggered by namespace access below. |
| 18 | +globalThis.eval_list = []; |
| 19 | + |
| 20 | +// Additionally check that the exported properties `foo` and `identifier` |
| 21 | +// are defined and have their values assigned at this point. |
| 22 | +assert.strictEqual(imported.foo, 42); |
| 23 | +assert.strictEqual(imported.identifier, 'package-type-commonjs'); |
| 24 | + |
| 25 | +// Check that the module has been evaluated at this point, |
| 26 | +// also that it's not evaluated more than once. |
| 27 | +assert.deepStrictEqual(['defer-1'], globalThis.eval_list); |
| 28 | + |
| 29 | +// Clean-up |
| 30 | +delete globalThis.eval_list; |
0 commit comments