-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #251 from iambumblehead/add-support-for-optional-r…
…esolvers add support for custom resolvers
- Loading branch information
Showing
8 changed files
with
103 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,7 @@ | ||
const isMocked = false | ||
const isCustomResolverChild = true | ||
|
||
export default { | ||
isCustomResolverChild, | ||
isMocked | ||
} |
This file contains 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,9 @@ | ||
import child from 'RESOLVECUSTOM' | ||
import path from 'node:path' | ||
|
||
const pathbasenameresult = path.basename('/the/very/happy-dog.png') | ||
|
||
export { | ||
child, | ||
pathbasenameresult | ||
} |
This file contains 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,66 @@ | ||
import test from 'node:test' | ||
import assert from 'node:assert/strict' | ||
import module from 'node:module' | ||
import esmock from 'esmock' | ||
|
||
function resolverCustom (moduleId, parent) { | ||
parent = parent.replace(/\/tests\/.*$/, '/tests/tests-node/') | ||
|
||
// This logic looks unusual because of constraints here. This function must: | ||
// * must work at windows, where path.join and path.resolve cause issues | ||
// * must be string-serializable, no external funcions | ||
// * must resolve these moduleIds to corresponding, existing filepaths | ||
// * '../local/customResolverParent.js', | ||
// * 'RESOLVECUSTOM/ | ||
return ( | ||
/RESOLVECUSTOM$/.test(moduleId) | ||
? parent + '../local/customResolverChild.js' | ||
: parent + moduleId | ||
).replace(/\/tests-node\/\.\./, '') | ||
} | ||
|
||
async function resolve (specifier, context, next) { | ||
return next( | ||
specifier === 'RESOLVECUSTOM' | ||
? resolverCustom(specifier, context.parentURL) | ||
: specifier, context) | ||
} | ||
|
||
module.register && module.register(` | ||
data:text/javascript, | ||
${encodeURIComponent(resolverCustom)} | ||
export ${encodeURIComponent(resolve)}`.slice(1)) | ||
|
||
test('should use custom resolver', async () => { | ||
if (!module.register) | ||
return assert.ok('skip test') | ||
|
||
const customResolverParent = await esmock( | ||
'../local/customResolverParent.js', {}, { | ||
RESOLVECUSTOM: ({ isMocked: true }) | ||
}, { | ||
resolver: resolverCustom | ||
}) | ||
|
||
assert.ok(customResolverParent.child.isCustomResolverChild) | ||
assert.ok(customResolverParent.child.isMocked) | ||
}) | ||
|
||
test('should not call custom resover with builtin moduleIds', async () => { | ||
if (!module.register) | ||
return assert.ok('skip test') | ||
|
||
const customResolverParent = await esmock( | ||
'../local/customResolverParent.js', {}, { | ||
RESOLVECUSTOM: ({ isMocked: true }), | ||
path: { basename: () => 'basenametest' } | ||
}, { | ||
resolver: resolverCustom | ||
}) | ||
|
||
assert.ok(customResolverParent.child.isCustomResolverChild) | ||
assert.ok(customResolverParent.child.isMocked) | ||
assert.strictEqual( | ||
customResolverParent.pathbasenameresult, | ||
'basenametest') | ||
}) |