node/test/es-module/test-esm-assertionless-json-import.js
Antoine du Hamel d1ef6aa2db
esm: use import attributes instead of import assertions
The old import assertions proposal has been
renamed to "import attributes" with the follwing major changes:

1. The keyword is now `with` instead of `assert`.
2. Unknown assertions cause an error rather than being ignored,

This commit updates the documentation to encourage folks to use the new
syntax, and add aliases for module customization hooks.

PR-URL: https://github.com/nodejs/node/pull/50140
Fixes: https://github.com/nodejs/node/issues/50134
Refs: 159c82c5e6
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
2023-10-14 03:52:38 +00:00

82 lines
2.1 KiB
JavaScript

// Flags: --experimental-loader ./test/fixtures/es-module-loaders/assertionless-json-import.mjs
'use strict';
const common = require('../common');
const { strictEqual } = require('assert');
async function test() {
{
const [secret0, secret1] = await Promise.all([
import('../fixtures/experimental.json'),
import(
'../fixtures/experimental.json',
{ with: { type: 'json' } }
),
]);
strictEqual(secret0.default.ofLife, 42);
strictEqual(secret1.default.ofLife, 42);
strictEqual(secret0.default, secret1.default);
strictEqual(secret0, secret1);
}
{
const [secret0, secret1] = await Promise.all([
import('../fixtures/experimental.json?test'),
import(
'../fixtures/experimental.json?test',
{ with: { type: 'json' } }
),
]);
strictEqual(secret0.default.ofLife, 42);
strictEqual(secret1.default.ofLife, 42);
strictEqual(secret0.default, secret1.default);
strictEqual(secret0, secret1);
}
{
const [secret0, secret1] = await Promise.all([
import('../fixtures/experimental.json#test'),
import(
'../fixtures/experimental.json#test',
{ with: { type: 'json' } }
),
]);
strictEqual(secret0.default.ofLife, 42);
strictEqual(secret1.default.ofLife, 42);
strictEqual(secret0.default, secret1.default);
strictEqual(secret0, secret1);
}
{
const [secret0, secret1] = await Promise.all([
import('../fixtures/experimental.json?test2#test'),
import(
'../fixtures/experimental.json?test2#test',
{ with: { type: 'json' } }
),
]);
strictEqual(secret0.default.ofLife, 42);
strictEqual(secret1.default.ofLife, 42);
strictEqual(secret0.default, secret1.default);
strictEqual(secret0, secret1);
}
{
const [secret0, secret1] = await Promise.all([
import('data:application/json,{"ofLife":42}'),
import(
'data:application/json,{"ofLife":42}',
{ with: { type: 'json' } }
),
]);
strictEqual(secret0.default.ofLife, 42);
strictEqual(secret1.default.ofLife, 42);
}
}
test().then(common.mustCall());