| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 0998016 commit bca3367
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,30 @@ | |||
| 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; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,17 @@ | |||
| 1 | + // This fixture is imported as a module | ||
| 2 | + // in test/es-module/test-cjs-defer-static-import-eval.mjs | ||
| 3 | + // to ensure a CommonJS module imported with `import defer` | ||
| 4 | + // is only executed once. | ||
| 5 | + const assert = require('assert'); | ||
| 6 | + | ||
| 7 | + const identifier = 'package-type-commonjs'; | ||
| 8 | + | ||
| 9 | + module.exports.foo = 42; | ||
| 10 | + module.exports.identifier = identifier; | ||
| 11 | + | ||
| 12 | + // The `eval_list` is initialised by the importing module, | ||
| 13 | + // so by the time the fixture is executed, `eval_list` should | ||
| 14 | + // already be initialised. | ||
| 15 | + assert.deepEqual(globalThis.eval_list, []); | ||
| 16 | + | ||
| 17 | + globalThis.eval_list.push('defer-1'); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments