Skip to content

Commit a392f74

Browse files
committed
loader: --experimental-raw-imports flag tests
1 parent 66a1503 commit a392f74

5 files changed

+150
-0
lines changed

test/es-module/test-esm-import-attributes-errors.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ async function test() {
2626
{ code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE' }
2727
);
2828

29+
await assert.rejects(
30+
import(jsModuleDataUrl, { with: { type: 'text' } }),
31+
{ code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' }
32+
);
33+
34+
await assert.rejects(
35+
import(jsModuleDataUrl, { with: { type: 'bytes' } }),
36+
{ code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' }
37+
);
38+
2939
await assert.rejects(
3040
import(jsModuleDataUrl, { with: { type: 'json', other: 'unsupported' } }),
3141
{ code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' }

test/es-module/test-esm-import-attributes-errors.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ await assert.rejects(
2121
{ code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE' }
2222
);
2323

24+
await assert.rejects(
25+
import(jsModuleDataUrl, { with: { type: 'text' } }),
26+
{ code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' }
27+
);
28+
29+
await assert.rejects(
30+
import(jsModuleDataUrl, { with: { type: 'bytes' } }),
31+
{ code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' }
32+
);
33+
2434
await assert.rejects(
2535
import(jsModuleDataUrl, { with: { type: 'json', other: 'unsupported' } }),
2636
{ code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' }
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Flags: --expose-internals --experimental-raw-imports
2+
'use strict';
3+
require('../common');
4+
5+
const assert = require('assert');
6+
7+
const { validateAttributes } = require('internal/modules/esm/assert');
8+
9+
const url = 'test://';
10+
11+
// ... {type = 'text'}
12+
assert.ok(validateAttributes(url, 'text', { type: 'text' }));
13+
14+
assert.throws(() => validateAttributes(url, 'text', {}), {
15+
code: 'ERR_IMPORT_ATTRIBUTE_MISSING',
16+
});
17+
18+
assert.throws(() => validateAttributes(url, 'module', { type: 'text' }), {
19+
code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE',
20+
});
21+
22+
// ... {type = 'bytes'}
23+
assert.ok(validateAttributes(url, 'bytes', { type: 'bytes' }));
24+
25+
assert.throws(() => validateAttributes(url, 'bytes', {}), {
26+
code: 'ERR_IMPORT_ATTRIBUTE_MISSING',
27+
});
28+
29+
assert.throws(() => validateAttributes(url, 'module', { type: 'bytes' }), {
30+
code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE',
31+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import '../common/index.mjs';
2+
import assert from 'assert';
3+
4+
await assert.rejects(
5+
import('../fixtures/file-to-read-without-bom.txt', { with: { type: 'text' } }),
6+
{ code: 'ERR_UNKNOWN_FILE_EXTENSION' },
7+
);
8+
9+
await assert.rejects(
10+
import('data:text/plain,hello%20world', { with: { type: 'text' } }),
11+
{ code: 'ERR_UNKNOWN_MODULE_FORMAT' },
12+
);
13+
14+
await assert.rejects(
15+
import('../fixtures/file-to-read-without-bom.txt', { with: { type: 'bytes' } }),
16+
{ code: 'ERR_UNKNOWN_FILE_EXTENSION' },
17+
);
18+
19+
await assert.rejects(
20+
import('data:text/plain,hello%20world', { with: { type: 'bytes' } }),
21+
{ code: 'ERR_UNKNOWN_MODULE_FORMAT' },
22+
);
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Flags: --experimental-raw-imports
2+
import '../common/index.mjs';
3+
import assert from 'assert';
4+
import { Buffer } from 'buffer';
5+
6+
import staticText from '../fixtures/file-to-read-without-bom.txt' with { type: 'text' };
7+
import staticTextWithBOM from '../fixtures/file-to-read-with-bom.txt' with { type: 'text' };
8+
import staticBytes from '../fixtures/file-to-read-without-bom.txt' with { type: 'bytes' };
9+
10+
const expectedText = 'abc\ndef\nghi\n';
11+
const expectedBytes = Buffer.from(expectedText);
12+
13+
assert.strictEqual(staticText, expectedText);
14+
assert.strictEqual(staticTextWithBOM, expectedText);
15+
16+
assert.ok(staticBytes instanceof Uint8Array);
17+
assert.deepStrictEqual(Buffer.from(staticBytes), expectedBytes);
18+
19+
const dynamicText = await import('../fixtures/file-to-read-without-bom.txt', {
20+
with: { type: 'text' },
21+
});
22+
assert.strictEqual(dynamicText.default, expectedText);
23+
24+
const dynamicBytes = await import('../fixtures/file-to-read-without-bom.txt', {
25+
with: { type: 'bytes' },
26+
});
27+
assert.deepStrictEqual(Buffer.from(dynamicBytes.default), expectedBytes);
28+
29+
const dataText = await import('data:text/plain,hello%20world', {
30+
with: { type: 'text' },
31+
});
32+
assert.strictEqual(dataText.default, 'hello world');
33+
34+
const dataBytes = await import('data:text/plain,hello%20world', {
35+
with: { type: 'bytes' },
36+
});
37+
assert.strictEqual(Buffer.from(dataBytes.default).toString(), 'hello world');
38+
39+
const dataJsAsText = await import('data:text/javascript,export{}', {
40+
with: { type: 'text' },
41+
});
42+
assert.strictEqual(dataJsAsText.default, 'export{}');
43+
44+
const dataJsAsBytes = await import('data:text/javascript,export{}', {
45+
with: { type: 'bytes' },
46+
});
47+
assert.strictEqual(Buffer.from(dataJsAsBytes.default).toString(), 'export{}');
48+
49+
const dataInvalidUtf8 = await import('data:text/plain,%66%6f%80%6f', {
50+
with: { type: 'text' },
51+
});
52+
assert.strictEqual(dataInvalidUtf8.default, 'fo\ufffdo');
53+
54+
const jsAsText = await import('../fixtures/syntax/bad_syntax.js', {
55+
with: { type: 'text' },
56+
});
57+
assert.match(jsAsText.default, /^var foo bar;/);
58+
59+
const jsAsBytes = await import('../fixtures/syntax/bad_syntax.js', {
60+
with: { type: 'bytes' },
61+
});
62+
assert.match(Buffer.from(jsAsBytes.default).toString(), /^var foo bar;/);
63+
64+
const jsonAsText = await import('../fixtures/invalid.json', {
65+
with: { type: 'text' },
66+
});
67+
assert.match(jsonAsText.default, /"im broken"/);
68+
69+
await assert.rejects(
70+
import('data:text/plain,hello%20world'),
71+
{ code: 'ERR_UNKNOWN_MODULE_FORMAT' },
72+
);
73+
74+
await assert.rejects(
75+
import('../fixtures/file-to-read-without-bom.txt'),
76+
{ code: 'ERR_UNKNOWN_FILE_EXTENSION' },
77+
);

0 commit comments

Comments
 (0)