Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mock: allow absolute deps, not node builtins
Fix: #948
- Loading branch information
Showing
6 changed files
with
129 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { resolveImport } from 'resolve-import' | ||
|
||
export const resolveMockEntryPoint = async ( | ||
url: URL, | ||
module: string, | ||
serviceKey: string, | ||
key: string, | ||
caller: Function | ((...a: any[]) => any) | ||
): Promise<string> => { | ||
let mockedModuleURL: URL | ||
if (module.startsWith('./') || module.startsWith('../')) { | ||
mockedModuleURL = new URL(module, url) | ||
} else { | ||
const res = (await resolveImport(module, url)) as URL | ||
// caught at the exposed API, defense in depth only | ||
// but the experience if it throws here is unhelpful. | ||
/* c8 ignore start */ | ||
if (typeof res === 'string') { | ||
const er = new TypeError( | ||
'node builtins cannot be mock imported' | ||
) | ||
Error.captureStackTrace(er, caller) | ||
throw er | ||
} | ||
/* c8 ignore stop */ | ||
mockedModuleURL = res | ||
} | ||
mockedModuleURL.searchParams.set('tapmock', `${serviceKey}.${key}`) | ||
return String(mockedModuleURL) | ||
} |
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,30 @@ | ||
import { resolveImport } from 'resolve-import' | ||
import t from 'tap' | ||
|
||
import { resolveMockEntryPoint } from '../src/resolve-mock-entry-point.js' | ||
|
||
t.equal( | ||
await resolveMockEntryPoint( | ||
new URL(import.meta.url), | ||
'../dist/esm/hooks.mjs', | ||
'service-key', | ||
'mock-key', | ||
() => {} | ||
), | ||
String( | ||
await resolveImport('../dist/esm/hooks.mjs', import.meta.url) | ||
) + '?tapmock=service-key.mock-key' | ||
) | ||
|
||
t.equal( | ||
await resolveMockEntryPoint( | ||
new URL(import.meta.url), | ||
'@tapjs/synonyms', | ||
'service-key', | ||
'mock-key', | ||
() => {} | ||
), | ||
String( | ||
await resolveImport('@tapjs/synonyms', import.meta.url) | ||
) + '?tapmock=service-key.mock-key' | ||
) |