Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixup: pr comments
  • Loading branch information
bmeck committed Nov 15, 2021
commit 0e11bde2408e058e7dc86b71359adbefe7684787
6 changes: 4 additions & 2 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -809,8 +809,8 @@ const require = createRequire(cwd() + '/<preload>');
In order to allow communication between the application and the loader, another
argument is provided to the preload code: `port`. This is available as a
parameter to the loader hook and inside of the source text returned by the hook.
Some care must be taken in order to properly `ref()` and `unref()` the
`MessagePort` to prevent a process from being in a state where it won't close
Some care must be taken in order to properly call [`port.ref()`][] and
[`port.unref()`][] to prevent a process from being in a state where it won't close
normally.

```js
Expand Down Expand Up @@ -1404,6 +1404,8 @@ success!
[`module.createRequire()`]: module.md#modulecreaterequirefilename
[`module.syncBuiltinESMExports()`]: module.md#modulesyncbuiltinesmexports
[`package.json`]: packages.md#nodejs-packagejson-field-definitions
[`port.ref()`]: https://nodejs.org/dist/latest-v17.x/docs/api/worker_threads.html#portref
[`port.unref()`]: https://nodejs.org/dist/latest-v17.x/docs/api/worker_threads.html#portunref
[`process.dlopen`]: process.md#processdlopenmodule-filename-flags
[`string`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
[`util.TextDecoder`]: util.md#class-utiltextdecoder
Expand Down
6 changes: 6 additions & 0 deletions test/es-module/test-esm-loader-mock.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// Flags: --loader ./test/fixtures/es-module-loaders/mock-loader.mjs
import '../common/index.mjs';
import assert from 'assert/strict';

// This is provided by test/fixtures/es-module-loaders/mock-loader.mjs
import mock from 'node:mock';
Comment thread
bmeck marked this conversation as resolved.

mock('node:events', {
EventEmitter: 'This is mocked!'
});

// This resolves to node:events
// It is intercepted by mock-loader and doesn't return the normal value
assert.deepStrictEqual(await import('events'), Object.defineProperty({
Comment on lines +10 to +14
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It resolves to node:events, you mean, it resolves to the mocked version?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yes

__proto__: null,
EventEmitter: 'This is mocked!'
Expand All @@ -20,6 +23,9 @@ const mutator = mock('node:events', {
EventEmitter: 'This is mocked v2!'
});

// It is intercepted by mock-loader and doesn't return the normal value.
// This is resolved separately from the import above since the specifiers
// are different.
const mockedV2 = await import('node:events');
assert.deepStrictEqual(mockedV2, Object.defineProperty({
__proto__: null,
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/es-module-loaders/mock-loader.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function globalPreload({port}) {
port.postMessage({ mockVersion, resolved, exports });
return namespace;
}
setImportMetaCallback((meta, context, parent) => {
setImportMetaCallback((meta, context, defaultImportMetaInitializer) => {
if (context.url === 'node:mock') {
meta.doMock = doMock;
return;
Expand All @@ -69,7 +69,7 @@ export function globalPreload({port}) {
return;
}
}
parent(meta, context);
defaultImportMetaInitializer(meta, context);
});
};
return `(${insideAppContext})(getBuiltin, port, setImportMetaCallback)`
Comment thread
bmeck marked this conversation as resolved.
Expand Down