Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test,esm: validate more edge cases for dynamic imports #46059

Merged
merged 2 commits into from Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
86 changes: 85 additions & 1 deletion test/es-module/test-esm-loader-hooks.mjs
Expand Up @@ -4,7 +4,7 @@ import assert from 'node:assert';
import { execPath } from 'node:process';
import { describe, it } from 'node:test';

describe('Loader hooks', () => {
describe('Loader hooks', { concurrency: true }, () => {
it('are called with all expected arguments', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
Expand All @@ -23,4 +23,88 @@ describe('Loader hooks', () => {
assert.match(lines[2], /{"url":"file:\/\/\/.*\/experimental\.json","format":"test","shortCircuit":true}/);
assert.match(lines[3], /{"source":{"type":"Buffer","data":\[.*\]},"format":"json","shortCircuit":true}/);
});

it('should be able to handle never resolving dynamic imports (ESM entry point)', async () => {
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('es-module-loaders/never-settling-resolve-step/loader.mjs'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for this PR, but I've noticed that in most uses of fixtures.fileURL we treat it as if it's returning a string when in fact it's a URL, and I guess it's getting cast to a string here. We might want to change the function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it matter? FWIW I prefer it returning a URL as now.

fixtures.path('es-module-loaders/never-settling-resolve-step/never-resolve.mjs'),
]);

assert.strictEqual(stderr, '');
assert.match(stdout, /^should be output\r?\n$/);
assert.strictEqual(code, 13);
assert.strictEqual(signal, null);
});

it('should be able to handle never resolving dynamic imports (CJS entry point)', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('es-module-loaders/never-settling-resolve-step/loader.mjs'),
fixtures.path('es-module-loaders/never-settling-resolve-step/never-resolve.cjs'),
]);

assert.strictEqual(stderr, '');
assert.match(stdout, /^should be output\r?\n$/);
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
});

it('should be able to handle never loading dynamic imports (ESM entry point)', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('es-module-loaders/never-settling-resolve-step/loader.mjs'),
fixtures.path('es-module-loaders/never-settling-resolve-step/never-load.mjs'),
]);

assert.strictEqual(stderr, '');
assert.match(stdout, /^should be output\r?\n$/);
assert.strictEqual(code, 13);
assert.strictEqual(signal, null);
});

it('should be able to handle never loading dynamic imports (CJS entry point)', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('es-module-loaders/never-settling-resolve-step/loader.mjs'),
fixtures.path('es-module-loaders/never-settling-resolve-step/never-load.cjs'),
]);

assert.strictEqual(stderr, '');
assert.match(stdout, /^should be output\r?\n$/);
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
});

it('should be able to handle race of never settling dynamic imports (ESM entry point)', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('es-module-loaders/never-settling-resolve-step/loader.mjs'),
fixtures.path('es-module-loaders/never-settling-resolve-step/race.mjs'),
]);

assert.strictEqual(stderr, '');
assert.match(stdout, /^true\r?\n$/);
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
});

it('should be able to handle race of never settling dynamic imports (CJS entry point)', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('es-module-loaders/never-settling-resolve-step/loader.mjs'),
fixtures.path('es-module-loaders/never-settling-resolve-step/race.cjs'),
]);

assert.strictEqual(stderr, '');
assert.match(stdout, /^true\r?\n$/);
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
});
});
@@ -0,0 +1,10 @@
export function resolve(specifier, context, next) {
if (specifier === 'never-settle-resolve') return new Promise(() => {});
if (specifier === 'never-settle-load') return { __proto__: null, shortCircuit: true, url: 'never-settle:///' };
return next(specifier, context);
}

export function load(url, context, next) {
if (url === 'never-settle:///') return new Promise(() => {});
return next(url, context);
}
@@ -0,0 +1,7 @@
'use strict';

const neverSettlingDynamicImport = import('never-settle-load');

console.log('should be output');

neverSettlingDynamicImport.then(() => process.exit(1));
@@ -0,0 +1,5 @@
const neverSettlingDynamicImport = import('never-settle-load');

console.log('should be output');

await neverSettlingDynamicImport;
@@ -0,0 +1,7 @@
'use strict';

const neverSettlingDynamicImport = import('never-settle-resolve');

console.log('should be output');

neverSettlingDynamicImport.then(() => process.exit(1));
@@ -0,0 +1,5 @@
const neverSettlingDynamicImport = import('never-settle-resolve');

console.log('should be output');

await neverSettlingDynamicImport;
@@ -0,0 +1,7 @@
'use strict';

Promise.race([
import('never-settle-resolve'),
import('never-settle-load'),
import('node:process'),
]).then(result => console.log(result.default === process));
@@ -0,0 +1,7 @@
const result = await Promise.race([
import('never-settle-resolve'),
import('never-settle-load'),
import('node:process'),
]);

console.log(result.default === process);