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

esm: misc test refactors #46631

Merged
merged 3 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions test/es-module/test-esm-import-meta-resolve.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { mustCall } from '../common/index.mjs';
import assert from 'assert';

const dirname = import.meta.url.slice(0, import.meta.url.lastIndexOf('/') + 1);
const fixtures = dirname.slice(0, dirname.lastIndexOf('/', dirname.length - 2) +
1) + 'fixtures/';
const fixtures = dirname.slice(0, dirname.lastIndexOf('/', dirname.length - 2) + 1) + 'fixtures/';

(async () => {
assert.strictEqual(await import.meta.resolve('./test-esm-import-meta.mjs'),
Expand Down
2 changes: 1 addition & 1 deletion test/es-module/test-esm-initialization.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { execPath } from 'node:process';
import { describe, it } from 'node:test';


describe('ESM: ensure initialisation happens only once', { concurrency: true }, () => {
describe('ESM: ensure initialization happens only once', { concurrency: true }, () => {
it(async () => {
const { code, stderr, stdout } = await spawnPromisified(execPath, [
'--loader',
Expand Down
6 changes: 3 additions & 3 deletions test/es-module/test-esm-loader-chaining.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const setupArgs = [
'--input-type=module',
'--eval',
];
const commonInput = 'import fs from "node:fs"; console.log(fs)';
const commonInput = 'import os from "node:os"; console.log(os)';
const commonArgs = [
...setupArgs,
commonInput,
Expand Down Expand Up @@ -114,11 +114,11 @@ describe('ESM: loader chaining', { concurrency: true }, () => {
);

assert.match(stdout, /^resolve arg count: 3$/m);
assert.match(stdout, /specifier: 'node:fs'/);
assert.match(stdout, /specifier: 'node:os'/);
assert.match(stdout, /next: \[AsyncFunction: nextResolve\]/);

assert.match(stdout, /^load arg count: 3$/m);
assert.match(stdout, /url: 'node:fs'/);
assert.match(stdout, /url: 'node:os'/);
assert.match(stdout, /next: \[AsyncFunction: nextLoad\]/);
});

Expand Down
12 changes: 12 additions & 0 deletions test/es-module/test-esm-loader-event-loop.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Flags: --experimental-loader ./test/fixtures/es-module-loaders/hooks-custom.mjs
import { mustCall } from '../common/index.mjs';

const done = mustCall();


// Test that the process doesn't exit because of a caught exception thrown as part of dynamic import().
for (let i = 0; i < 10; i++) {
await import('nonexistent/file.mjs').catch(() => {});
}

done();
288 changes: 288 additions & 0 deletions test/es-module/test-esm-loader-spawn-promisified.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
import { spawnPromisified } from '../common/index.mjs';
import * as fixtures from '../common/fixtures.mjs';
import assert from 'node:assert';
import { execPath } from 'node:process';
import { describe, it } from 'node:test';

describe('Loader hooks throwing errors', { concurrency: true }, () => {
it('throws on nonexistent modules', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('/es-module-loaders/hooks-custom.mjs'),
'--input-type=module',
'--eval',
'import "nonexistent/file.mjs"',
]);

assert.strictEqual(code, 1);
assert.strictEqual(signal, null);
assert.strictEqual(stdout, '');
assert.match(stderr, /ERR_MODULE_NOT_FOUND/);
});

it('throws on unknown extensions', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('/es-module-loaders/hooks-custom.mjs'),
'--input-type=module',
'--eval',
`import '${fixtures.fileURL('/es-modules/file.unknown')}'`,
]);

assert.strictEqual(code, 1);
assert.strictEqual(signal, null);
assert.strictEqual(stdout, '');
assert.match(stderr, /ERR_UNKNOWN_FILE_EXTENSION/);
});

it('throws on invalid return values', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('/es-module-loaders/hooks-custom.mjs'),
'--input-type=module',
'--eval',
'import "esmHook/badReturnVal.mjs"',
]);

assert.strictEqual(code, 1);
assert.strictEqual(signal, null);
assert.strictEqual(stdout, '');
assert.match(stderr, /ERR_INVALID_RETURN_VALUE/);
});

it('throws on boolean false', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('/es-module-loaders/hooks-custom.mjs'),
'--input-type=module',
'--eval',
'import "esmHook/format.false"',
]);

assert.strictEqual(code, 1);
assert.strictEqual(signal, null);
assert.strictEqual(stdout, '');
assert.match(stderr, /ERR_INVALID_RETURN_PROPERTY_VALUE/);
});
it('throws on boolean true', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('/es-module-loaders/hooks-custom.mjs'),
'--input-type=module',
'--eval',
'import "esmHook/format.true"',
]);

assert.strictEqual(code, 1);
assert.strictEqual(signal, null);
assert.strictEqual(stdout, '');
assert.match(stderr, /ERR_INVALID_RETURN_PROPERTY_VALUE/);
});

it('throws on invalid returned object', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('/es-module-loaders/hooks-custom.mjs'),
'--input-type=module',
'--eval',
'import "esmHook/badReturnFormatVal.mjs"',
]);

assert.strictEqual(code, 1);
assert.strictEqual(signal, null);
assert.strictEqual(stdout, '');
assert.match(stderr, /ERR_INVALID_RETURN_PROPERTY_VALUE/);
});

it('throws on unsupported format', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('/es-module-loaders/hooks-custom.mjs'),
'--input-type=module',
'--eval',
'import "esmHook/unsupportedReturnFormatVal.mjs"',
]);

assert.strictEqual(code, 1);
assert.strictEqual(signal, null);
assert.strictEqual(stdout, '');
assert.match(stderr, /ERR_UNKNOWN_MODULE_FORMAT/);
});

it('throws on invalid format property type', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('/es-module-loaders/hooks-custom.mjs'),
'--input-type=module',
'--eval',
'import "esmHook/badReturnSourceVal.mjs"',
]);

assert.strictEqual(code, 1);
assert.strictEqual(signal, null);
assert.strictEqual(stdout, '');
assert.match(stderr, /ERR_INVALID_RETURN_PROPERTY_VALUE/);
});

it('rejects dynamic imports for all of the error cases checked above', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('/es-module-loaders/hooks-custom.mjs'),
'--input-type=module',
'--eval',
`import assert from 'node:assert';
await Promise.allSettled([
import('nonexistent/file.mjs'),
import('${fixtures.fileURL('/es-modules/file.unknown')}'),
import('esmHook/badReturnVal.mjs'),
import('esmHook/format.false'),
import('esmHook/format.true'),
import('esmHook/badReturnFormatVal.mjs'),
import('esmHook/unsupportedReturnFormatVal.mjs'),
import('esmHook/badReturnSourceVal.mjs'),
]).then((results) => {
assert.strictEqual(results.every((result) => result.status === 'rejected'), true);
})`,
]);

assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
assert.strictEqual(stdout, '');
assert.strictEqual(stderr, '');
});
});

describe('Loader hooks parsing modules', { concurrency: true }, () => {
it('can parse .js files as ESM', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('/es-module-loaders/hooks-custom.mjs'),
'--input-type=module',
'--eval',
`import assert from 'node:assert';
await import('${fixtures.fileURL('/es-module-loaders/js-as-esm.js')}')
.then((parsedModule) => {
assert.strictEqual(typeof parsedModule, 'object');
assert.strictEqual(parsedModule.namedExport, 'named-export');
})`,
]);

assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
assert.strictEqual(stdout, '');
assert.strictEqual(stderr, '');
});

it('can define .ext files as ESM', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('/es-module-loaders/hooks-custom.mjs'),
'--input-type=module',
'--eval',
`import assert from 'node:assert';
await import('${fixtures.fileURL('/es-modules/file.ext')}')
.then((parsedModule) => {
assert.strictEqual(typeof parsedModule, 'object');
const { default: defaultExport } = parsedModule;
assert.strictEqual(typeof defaultExport, 'function');
assert.strictEqual(defaultExport.name, 'iAmReal');
assert.strictEqual(defaultExport(), true);
})`,
]);

assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
assert.strictEqual(stdout, '');
assert.strictEqual(stderr, '');
});

it('can predetermine the format in the custom loader resolve hook', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('/es-module-loaders/hooks-custom.mjs'),
'--input-type=module',
'--eval',
`import assert from 'node:assert';
await import('esmHook/preknownFormat.pre')
.then((parsedModule) => {
assert.strictEqual(typeof parsedModule, 'object');
assert.strictEqual(parsedModule.default, 'hello world');
})`,
]);

assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
assert.strictEqual(stdout, '');
assert.strictEqual(stderr, '');
});

it('can provide source for a nonexistent file', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('/es-module-loaders/hooks-custom.mjs'),
'--input-type=module',
'--eval',
`import assert from 'node:assert';
await import('esmHook/virtual.mjs')
.then((parsedModule) => {
assert.strictEqual(typeof parsedModule, 'object');
assert.strictEqual(typeof parsedModule.default, 'undefined');
assert.strictEqual(parsedModule.message, 'WOOHOO!');
})`,
]);

assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
assert.strictEqual(stdout, '');
assert.strictEqual(stderr, '');
});

it('ensures that loaders have a separate context from userland', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('/es-module-loaders/hooks-custom.mjs'),
'--input-type=module',
'--eval',
`import assert from 'node:assert';
await import('${fixtures.fileURL('/es-modules/stateful.mjs')}')
.then(({ default: count }) => {
assert.strictEqual(count(), 1);
});`,
]);

assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
assert.strictEqual(stdout, '');
assert.strictEqual(stderr, '');
});

it('ensures that user loaders are not bound to the internal loader', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('/es-module-loaders/loader-this-value-inside-hook-functions.mjs'),
'--input-type=module',
'--eval',
';', // Actual test is inside the loader module.
]);

assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
assert.strictEqual(stdout, '');
assert.strictEqual(stderr, '');
GeoffreyBooth marked this conversation as resolved.
Show resolved Hide resolved
});
});
Loading