Skip to content

Commit 69a8b3b

Browse files
committed
module: add two tests for eager imports when using import defer
- when the defer imported module contains top-level await - when the defer imported module is eagerly imported from another module Refs: https://github.com/tc39/proposal-defer-import-eval Signed-off-by: Maya Lekova <maya@igalia.com>
1 parent bee4016 commit 69a8b3b

4 files changed

Lines changed: 59 additions & 3 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+
// Tests that defer import of a module with top-level await
4+
// eagerly evaluates the imported module.
5+
6+
import '../common/index.mjs';
7+
import * as assert from 'assert';
8+
9+
// The importing module will only execute once its dependency containing
10+
// top-level await has its promises resolved, as per
11+
// https://github.com/tc39/proposal-top-level-await#what-exactly-is-blocked-by-a-top-level-await
12+
if (!globalThis.eval_list) {
13+
globalThis.eval_list = [];
14+
}
15+
16+
import defer * as ns from '../fixtures/es-modules/dep-module-top-level-await.mjs';
17+
18+
// Check that the module has already been evaluated.
19+
assert.strictEqual(globalThis.eval_list.length, 1);
20+
assert.strictEqual(ns.foo, 42);
21+
assert.strictEqual(globalThis.eval_list.length, 1);
22+
assert.partialDeepStrictEqual(['defer-tla-1'], globalThis.eval_list);
23+
24+
// Clean-up
25+
delete globalThis.eval_list;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Flags: --js-defer-import-eval
2+
3+
// Tests that defer import of a module that is also eagerly imported
4+
// from another dependency gets executed synchronously, and gets
5+
// executed exactly once.
6+
7+
import '../common/index.mjs';
8+
import * as assert from 'assert';
9+
10+
if (!globalThis.eval_list) {
11+
globalThis.eval_list = [];
12+
}
13+
14+
import * as non_deferred from '../fixtures/es-modules/module-with-module-tree.mjs';
15+
import defer * as deferred from '../fixtures/es-modules/module-deferred-eval.mjs';
16+
17+
// Check that the module has been evaluated exactly once.
18+
assert.strictEqual(globalThis.eval_list.length, 1);
19+
assert.strictEqual(deferred.foo, 42);
20+
assert.partialDeepStrictEqual(['defer-1'], globalThis.eval_list);
21+
22+
// Clean-up
23+
delete globalThis.eval_list;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { setTimeout } from 'node:timers/promises';
2+
3+
if (!globalThis.eval_list) {
4+
globalThis.eval_list = [];
5+
}
6+
7+
export const foo = 42;
8+
9+
await setTimeout(1);
10+
globalThis.eval_list.push('defer-tla-1');
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
if (!globalThis.eval_list) {
22
globalThis.eval_list = [];
33
}
4-
globalThis.eval_list.push('defer-1');
54

65
export const foo = 42;
7-
8-
console.log('executed');
6+
globalThis.eval_list.push('defer-1');

0 commit comments

Comments
 (0)