-
Notifications
You must be signed in to change notification settings - Fork 18
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
Add overloads for mock functions, fully expand out d.ts file #122
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
25a5a52
these changes allow the submitted tests to pass
bumblehead dc7a79e
remove semicolons and allow lint to pass
bumblehead 10019bf
added sourcemap tests
bumblehead e7bad16
gitignore dist and reference local esmock
bumblehead f2060ba
added gitignore inside tests-source-map
bumblehead ef1f862
add reference to tyes file in tests package.json
bumblehead 2686942
correct export definition in tests package.json
bumblehead b8aa956
correct export definition in tests package.json
bumblehead e9327d1
hard copy esmock d.ts file into tests
bumblehead ef706ea
use parent to define calleePath, per review
bumblehead 86c6db6
lint --fix .
bumblehead 2f25865
restore npm script source-map test
bumblehead 267573e
log resolvewithplus params to collect info
bumblehead 09052b1
remove console.log
bumblehead 7651cf6
Add overloads for mock functions, fully expand out d.ts file
jakebailey 945a808
Add options type
jakebailey 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 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; | ||
Comment on lines
+23
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These could be documented; not sure quite what to write as personally I would suggest that people use the named functions to set these instead (i.e. treat options as an internal implementation detail). |
||
} | ||
|
||
/** | ||
* 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"] | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This date's now wrong; feel free to change.