Skip to content

Commit c6bb77a

Browse files
committed
test: add smoke tests for defer importing synthetic modules
The tests added ensure that Node.js doesn't crash or produce incorrect results when importing synthetic modules (i.e. JSON, text or builtin modules) with the `defer` modifier. Signed-off-by: Maya Lekova <maya@igalia.com>
1 parent 46a7dbd commit c6bb77a

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Flags: --js-defer-import-eval --experimental-import-text --expose-internals
2+
3+
// Test that uses import.defer for a builtin module. Currently
4+
// defer importing of a synthetic module should be a no-op
5+
// in Node.js, so the test is mostly a smoke test that Node
6+
// doesn't crash.
7+
8+
import '../common/index.mjs';
9+
import * as assert from 'assert';
10+
11+
import binding from 'internal/test/binding';
12+
const { kEvaluated } = binding.internalBinding('module_wrap');
13+
const helpers = await import('internal/modules/helpers');
14+
15+
// Load the http builtin module and check that it is evaluated.
16+
let builtin = helpers.default.loadBuiltinModule('http');
17+
let wrap = builtin.getESMFacade();
18+
assert.strictEqual(wrap.getStatus(), kEvaluated);
19+
20+
// Import the http builtin module.
21+
import defer * as http from 'node:http';
22+
http.STATUS_CODES;
23+
24+
// Refresh the references to the builtin module and check again its status.
25+
builtin = helpers.default.loadBuiltinModule('http');
26+
wrap = builtin.getESMFacade();
27+
assert.strictEqual(wrap.getStatus(), kEvaluated);
28+
29+
// Check that the imported module contains some known properties.
30+
assert.notStrictEqual(http.STATUS_CODES, undefined);
31+
assert.notStrictEqual(http.createServer, undefined);
32+
assert.strictEqual(typeof http.createServer, 'function');
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Flags: --js-defer-import-eval
2+
3+
// Test that uses import.defer for a JSON module. Currently
4+
// defer importing of a synthetic module should be a no-op
5+
// in Node.js, so the test is mostly a smoke test that Node
6+
// doesn't crash.
7+
8+
import '../common/index.mjs';
9+
import * as assert from 'assert';
10+
11+
import defer * as imported_json
12+
from '../fixtures/json-with-directory-name-module/module-stub.json'
13+
with { type: 'json' };
14+
15+
// Check that the imported object has the expected key/value.
16+
assert.strictEqual(imported_json.default.rocko, 'artischocko');
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Flags: --js-defer-import-eval --experimental-import-text
2+
3+
// Test that uses import.defer for a text module. Currently
4+
// defer importing of a synthetic module should be a no-op
5+
// in Node.js, so the test is mostly a smoke test that Node
6+
// doesn't crash.
7+
8+
import '../common/index.mjs';
9+
import * as assert from 'assert';
10+
11+
import defer * as imported_text
12+
from '../fixtures/file-to-read-without-bom.txt'
13+
with { type: 'text' };
14+
15+
const expected_text = 'abc\ndef\nghi\n';
16+
17+
// Check that the imported text has the expected value.
18+
assert.strictEqual(imported_text.default, expected_text);

0 commit comments

Comments
 (0)