node/test/es-module/test-esm-import-assertion-warning.mjs
Wei Zhu c0aebed4b3
esm: fix inconsistency with importAssertion in resolve hook
As the documentation states, the `context.importAssertion` should be
still supported and emit a warning. This is true for the `load` hook,
but not correct for context of the `resolve` hook.

This commit fixes the inconsistency.

PR-URL: https://github.com/nodejs/node/pull/55365
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2024-10-15 21:02:07 +02:00

43 lines
1.8 KiB
JavaScript

import { spawnPromisified } from '../common/index.mjs';
import assert from 'node:assert';
import { execPath } from 'node:process';
await Promise.all([
// Using importAssertions in the resolve hook should warn but still work.
`data:text/javascript,export ${encodeURIComponent(function resolve() {
return { shortCircuit: true, url: 'data:application/json,1', importAssertions: { type: 'json' } };
})}`,
// Using importAssertions on the context object of the resolve hook should warn but still work.
`data:text/javascript,export ${encodeURIComponent(function resolve(s, c, n) {
const type = c.importAssertions.type;
return { shortCircuit: true, url: 'data:application/json,1', importAttributes: { type: type ?? 'json' } };
})}`,
// Setting importAssertions on the context object of the load hook should warn but still work.
`data:text/javascript,export ${encodeURIComponent(function load(u, c, n) {
c.importAssertions = { type: 'json' };
return n('data:application/json,1', c);
})}`,
// Creating a new context object with importAssertions in the load hook should warn but still work.
`data:text/javascript,export ${encodeURIComponent(function load(u, c, n) {
return n('data:application/json,1', { importAssertions: { type: 'json' } });
})}`,
].map(async (loaderURL) => {
const { stdout, stderr, code } = await spawnPromisified(execPath, [
'--input-type=module',
'--eval', `
import assert from 'node:assert';
import { register } from 'node:module';
register(${JSON.stringify(loaderURL)});
assert.deepStrictEqual(
{ ...await import('data:') },
{ default: 1 }
);`,
]);
assert.match(stderr, /Use `importAttributes` instead of `importAssertions`/);
assert.strictEqual(stdout, '');
assert.strictEqual(code, 0);
}));