Skip to content

Commit a006262

Browse files
authored
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 also eagerly imported Refs: https://github.com/tc39/proposal-defer-import-eval Signed-off-by: Maya Lekova <maya@igalia.com> PR-URL: #64197 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 1912893 commit a006262

4 files changed

Lines changed: 58 additions & 3 deletions

File tree

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 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+
globalThis.eval_list ||= [];
13+
14+
import defer * as ns from '../fixtures/es-modules/dep-module-top-level-await.mjs';
15+
16+
// Check that the module has already been evaluated.
17+
assert.strictEqual(globalThis.eval_list.length, 1);
18+
assert.strictEqual(ns.foo, 42);
19+
assert.strictEqual(globalThis.eval_list.length, 1);
20+
assert.partialDeepStrictEqual(['defer-tla-1'], globalThis.eval_list);
21+
22+
// Clean-up
23+
delete globalThis.eval_list;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
globalThis.eval_list ||= [];
11+
12+
import * as non_deferred from '../fixtures/es-modules/module-with-module-tree.mjs';
13+
import defer * as deferred from '../fixtures/es-modules/module-deferred-eval.mjs';
14+
15+
// Check that the module has been evaluated exactly once.
16+
assert.strictEqual(globalThis.eval_list.length, 1);
17+
assert.strictEqual(deferred.foo, 42);
18+
assert.partialDeepStrictEqual(['defer-1'], globalThis.eval_list);
19+
20+
// Skip linter warning
21+
assert.strictEqual(non_deferred.bar, 64);
22+
23+
// Clean-up
24+
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)