Skip to content

Commit 8a02ad5

Browse files
committed
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 from its exports. Signed-off-by: Maya Lekova <maya@igalia.com>
1 parent 8cf16b3 commit 8a02ad5

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
7+
import '../common/index.mjs';
8+
import * as assert from 'assert';
9+
10+
// Import the CJS module with the `defer` modifier.
11+
import defer * as imported from '../fixtures/es-modules/module-cjs-deferred-eval.js';
12+
13+
globalThis.eval_list = [];
14+
15+
// Check that the exported properties are accessible and have the
16+
// expected values.
17+
assert.strictEqual(imported.foo, 42);
18+
assert.strictEqual(imported.identifier, 'package-type-commonjs');
19+
20+
// Check that the module has been evaluated at this point,
21+
// also that it's not evaluated more than once.
22+
assert.deepStrictEqual(['defer-1'], globalThis.eval_list);
23+
24+
// Clean-up
25+
delete globalThis.eval_list;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff 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');

0 commit comments

Comments
 (0)