Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions test/es-module/test-defer-import-eval-with-tla.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Flags: --js-defer-import-eval

// Tests that defer import of a module with top-level await
// eagerly evaluates the imported module.

import '../common/index.mjs';
import * as assert from 'assert';

// The importing module will only execute once its dependency containing
// top-level await has its promises resolved, as per
// https://github.com/tc39/proposal-top-level-await#what-exactly-is-blocked-by-a-top-level-await
if (!globalThis.eval_list) {

Check failure on line 12 in test/es-module/test-defer-import-eval-with-tla.mjs

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'if' statement can be replaced with a logical operator assignment with operator ||=
globalThis.eval_list = [];
}

import defer * as ns from '../fixtures/es-modules/dep-module-top-level-await.mjs';

// Check that the module has already been evaluated.
assert.strictEqual(globalThis.eval_list.length, 1);
assert.strictEqual(ns.foo, 42);
assert.strictEqual(globalThis.eval_list.length, 1);
assert.partialDeepStrictEqual(['defer-tla-1'], globalThis.eval_list);
Comment thread
MayaLekova marked this conversation as resolved.

// Clean-up
delete globalThis.eval_list;
24 changes: 24 additions & 0 deletions test/es-module/test-defer-import-with-regular-import.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Flags: --js-defer-import-eval

// Tests that defer import of a module that is also eagerly imported
// from another dependency gets executed synchronously, and gets
// executed exactly once.

import '../common/index.mjs';
import * as assert from 'assert';

globalThis.eval_list ||= [];

import * as non_deferred from '../fixtures/es-modules/module-with-module-tree.mjs';
import defer * as deferred from '../fixtures/es-modules/module-deferred-eval.mjs';

// Check that the module has been evaluated exactly once.
assert.strictEqual(globalThis.eval_list.length, 1);
assert.strictEqual(deferred.foo, 42);
assert.partialDeepStrictEqual(['defer-1'], globalThis.eval_list);

// Skip linter warning
assert.strictEqual(non_deferred.bar, 64);

// Clean-up
delete globalThis.eval_list;
10 changes: 10 additions & 0 deletions test/fixtures/es-modules/dep-module-top-level-await.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { setTimeout } from 'node:timers/promises';

if (!globalThis.eval_list) {
globalThis.eval_list = [];
}

export const foo = 42;

await setTimeout(1);
globalThis.eval_list.push('defer-tla-1');
4 changes: 1 addition & 3 deletions test/fixtures/es-modules/module-deferred-eval.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
if (!globalThis.eval_list) {
globalThis.eval_list = [];
}
globalThis.eval_list.push('defer-1');

export const foo = 42;

console.log('executed');
globalThis.eval_list.push('defer-1');
Loading