-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
working mock test #39240
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
Closed
Closed
working mock test #39240
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
13ab19e
esm: working mock test
bmeck ffdaa30
fixup: Update doc/api/esm.md
bmeck f5e96f3
fixup: Apply suggestions from code review
bmeck f923cce
fixup: rebase fixes
bmeck 0e1e124
fixup: rebasing
bmeck 4e4ed13
fixup: Apply suggestions from code review
bmeck 8369c4a
fixup: Apply code review suggestion to avoid string template
bmeck 57ace81
fixup: lint
bmeck 7416b27
fixup: bad review change
bmeck d0276c6
fixup: Update test/fixtures/es-module-loaders/mock-loader.mjs
bmeck 0e11bde
fixup: pr comments
bmeck 172c038
fixup: linter
bmeck b9e1380
fixup: review nit, inspector debuggability
bmeck 5ad4c1b
fixup: doc the mock
bmeck d61b7d4
fixup: doc nit
bmeck 93ba88a
fixup: Apply suggestions from code review
bmeck 1c5996d
fixup: review nits
bmeck b9f339f
fixup: lint
bmeck 05e71c9
fixup: test doc clarification
bmeck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
bmeck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const { getOptionValue } = require('internal/options'); | ||
const experimentalImportMetaResolve = | ||
getOptionValue('--experimental-import-meta-resolve'); | ||
const { PromisePrototypeThen, PromiseReject } = primordials; | ||
const asyncESM = require('internal/process/esm_loader'); | ||
|
||
function createImportMetaResolve(defaultParentUrl) { | ||
return async function resolve(specifier, parentUrl = defaultParentUrl) { | ||
return PromisePrototypeThen( | ||
asyncESM.esmLoader.resolve(specifier, parentUrl), | ||
bmeck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
({ url }) => url, | ||
(error) => ( | ||
error.code === 'ERR_UNSUPPORTED_DIR_IMPORT' ? | ||
error.url : PromiseReject(error)) | ||
); | ||
}; | ||
} | ||
|
||
function initializeImportMeta(meta, context) { | ||
const url = context.url; | ||
|
||
// Alphabetical | ||
if (experimentalImportMetaResolve) | ||
meta.resolve = createImportMetaResolve(url); | ||
bmeck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
meta.url = url; | ||
} | ||
|
||
module.exports = { | ||
initializeImportMeta | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// 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'; | ||
bmeck marked this conversation as resolved.
Show resolved
Hide 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({ | ||
__proto__: null, | ||
EventEmitter: 'This is mocked!' | ||
}, Symbol.toStringTag, { | ||
enumerable: false, | ||
value: 'Module' | ||
})); | ||
|
||
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, | ||
EventEmitter: 'This is mocked v2!' | ||
}, Symbol.toStringTag, { | ||
enumerable: false, | ||
value: 'Module' | ||
})); | ||
|
||
mutator.EventEmitter = 'This is mocked v3!'; | ||
assert.deepStrictEqual(mockedV2, Object.defineProperty({ | ||
__proto__: null, | ||
EventEmitter: 'This is mocked v3!' | ||
}, Symbol.toStringTag, { | ||
enumerable: false, | ||
value: 'Module' | ||
})); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.