-
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 #122 from jakebailey/parent-param
Add overloads for mock functions, fully expand out d.ts file
- Loading branch information
Showing
12 changed files
with
215 additions
and
8 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 |
---|---|---|
@@ -1,2 +1,76 @@ | ||
declare function esmock(path: string, localmock?: any, globalmock?: any): any; | ||
/** | ||
* Mocks imports for the module specified by {@link modulePath}. | ||
* | ||
* The provided mocks replace the imported modules _fully_. | ||
* | ||
* @param modulePath The module whose imports will be mocked. | ||
* @param parent A URL to resolve specifiers relative to; typically `import.meta.url`. | ||
* If not specified, it will be inferred via the stack, which may not work | ||
* if source maps are in use. | ||
* @param mockDefs A mapping of import specifiers to mocked module objects; these mocks will | ||
* only be used for imports resolved in the module specified by {@link modulePath}. | ||
* @param globalDefs A mapping of import specifiers to mocked module objects; these mocks will | ||
* apply to imports within the module specified by {@link modulePath}, as well | ||
* as any transitively imported modules. | ||
* @param opt | ||
* @returns The result of importing {@link modulePath}, similar to `import(modulePath)`. | ||
*/ | ||
declare function esmock(modulePath: string, parent: string, mockDefs?: Record<string, any>, globalDefs?: Record<string, any>, opt?: esmock.Options): any; | ||
declare function esmock(modulePath: string, mockDefs?: Record<string, any>, globalDefs?: Record<string, any>, opt?: esmock.Options): any; | ||
|
||
declare namespace esmock { | ||
interface Options { | ||
partial?: boolean | undefined; | ||
purge?: boolean | undefined; | ||
} | ||
|
||
/** | ||
* Mocks imports for the module specified by {@link modulePath}. | ||
* | ||
* The provided mocks replace the imported modules _partially_, allowing some exports to | ||
* be overridden while the rest are provided by the real module. | ||
* | ||
* @param modulePath The module whose imports will be mocked. | ||
* @param parent A URL to resolve specifiers relative to; typically `import.meta.url`. | ||
* If not specified, it will be inferred via the stack, which may not work | ||
* if source maps are in use. | ||
* @param mockDefs A mapping of import specifiers to mocked module objects; these mocks will | ||
* only be used for imports resolved in the module specified by {@link modulePath}. | ||
* @param globalDefs A mapping of import specifiers to mocked module objects; these mocks will | ||
* apply to imports within the module specified by {@link modulePath}, as well | ||
* as any transitively imported modules. | ||
* @param opt | ||
* @returns The result of importing {@link modulePath}, similar to `import(modulePath)`. | ||
*/ | ||
function px(modulePath: string, parent: string, mockDefs?: Record<string, any>, globalDefs?: Record<string, any>, opt?: esmock.Options): any; | ||
function px(modulePath: string, mockDefs?: Record<string, any>, globalDefs?: Record<string, any>, opt?: esmock.Options): any; | ||
|
||
/** | ||
* Mocks dynamic imports for the module specified by {@link modulePath}. | ||
* | ||
* After using this function, consider calling {@link esmock.purge} to free memory. | ||
* | ||
* @param modulePath The module whose imports will be mocked. | ||
* @param parent A URL to resolve specifiers relative to; typically `import.meta.url`. | ||
* If not specified, it will be inferred via the stack, which may not work | ||
* if source maps are in use. | ||
* @param mockDefs A mapping of import specifiers to mocked module objects; these mocks will | ||
* only be used for imports resolved in the module specified by {@link modulePath}. | ||
* @param globalDefs A mapping of import specifiers to mocked module objects; these mocks will | ||
* apply to imports within the module specified by {@link modulePath}, as well | ||
* as any transitively imported modules. | ||
* @param opt | ||
* @returns The result of importing {@link modulePath}, similar to `import(modulePath)`. | ||
*/ | ||
function p(modulePath: string, parent: string, mockDefs?: Record<string, any>, globalDefs?: Record<string, any>, opt?: esmock.Options): any; | ||
function p(modulePath: string, mockDefs?: Record<string, any>, globalDefs?: Record<string, any>, opt?: esmock.Options): any; | ||
|
||
/** | ||
* Unregisters a dynamic mock created by {@link esmock.p}. | ||
* | ||
* @param mockModule A module object that was previously returned by {@link esmock.p}. | ||
*/ | ||
function purge(mockModule: any): void; | ||
} | ||
|
||
export default esmock; |
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,3 @@ | ||
import esmock from "../src/esmock.js"; | ||
|
||
export default esmock; |
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 @@ | ||
dist |
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 @@ | ||
# esmock-source-map-bug | ||
|
||
To run with source maps, run `npm run test`. esmock would previously fail unless the provided module path was absolute | ||
|
||
To run without source maps, run `npm run test-no-maps`. This works. | ||
|
||
https://github.com/iambumblehead/esmock/issues/113 |
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,34 @@ | ||
{ | ||
"type": "module", | ||
"description": "esmock unit tests, ava with sourcemap", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/iambumblehead/esmock.git" | ||
}, | ||
"dependencies": { | ||
"esmock": "file:..", | ||
"@ava/typescript": "^3.0.1", | ||
"@tsconfig/node14": "^1.0.3", | ||
"@types/node": "^18.7.3", | ||
"ava": "^4.3.1", | ||
"cross-env": "^7.0.3", | ||
"rimraf": "^3.0.2", | ||
"typescript": "^4.7.4" | ||
}, | ||
"scripts": { | ||
"test": "rimraf dist && tsc && cross-env NODE_OPTIONS=--loader=esmock NODE_NO_WARNINGS=1 ava", | ||
"test-no-maps": "rimraf dist && tsc --sourceMap false && cross-env NODE_OPTIONS=--loader=esmock NODE_NO_WARNINGS=1 ava" | ||
}, | ||
"ava": { | ||
"typescript": { | ||
"rewritePaths": { | ||
"src/": "dist/" | ||
}, | ||
"compile": false | ||
}, | ||
"environmentVariables": { | ||
"NO_COLOR": "1", | ||
"FORCE_COLOR": "0" | ||
} | ||
} | ||
} |
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 test from "ava"; | ||
import esmock from "esmock"; | ||
import { fileURLToPath } from "url"; | ||
|
||
import type * as indexType from "../index.js"; | ||
|
||
const expectedHostname = "my-machine"; | ||
|
||
test("using absolute path", async (t) => { | ||
const urlFull = fileURLToPath(new URL("../index.js", import.meta.url)); | ||
const indexModule = await esmock(urlFull, { | ||
os: { | ||
hostname: () => expectedHostname, | ||
}, | ||
}); | ||
|
||
const getHostname: typeof indexType.getHostname = indexModule.getHostname; | ||
t.is(getHostname(), expectedHostname); | ||
}); | ||
|
||
test("using relative path", async (t) => { | ||
const indexModule = await esmock("../index.js", import.meta.url, { | ||
os: { | ||
hostname: () => expectedHostname, | ||
}, | ||
}); | ||
|
||
const getHostname: typeof indexType.getHostname = indexModule.getHostname; | ||
t.is(getHostname(), expectedHostname); | ||
}); |
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,5 @@ | ||
import os from "os"; | ||
|
||
export function getHostname() { | ||
return os.hostname(); | ||
} |
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,23 @@ | ||
{ | ||
"extends": "@tsconfig/node14/tsconfig.json", | ||
"compilerOptions": { | ||
"module": "Node16", | ||
"moduleResolution": "Node16", | ||
"rootDir": "src", | ||
"outDir": "dist", | ||
"declarationDir": "dist", | ||
"declaration": true, | ||
"stripInternal": true, | ||
"sourceMap": true, | ||
|
||
"allowUnusedLabels": false, | ||
"allowUnreachableCode": false, | ||
"exactOptionalPropertyTypes": true, | ||
"noFallthroughCasesInSwitch": true, | ||
"noImplicitOverride": true, | ||
"noImplicitReturns": true, | ||
"noPropertyAccessFromIndexSignature": true, | ||
"importsNotUsedAsValues": "error" | ||
}, | ||
"exclude": ["dist"] | ||
} |