Skip to content
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

try to simplify esmock.d.ts types file #163

Merged
merged 5 commits into from
Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# changelog

* 2.0.3 _tbd_
* 2.0.3 _Sep.24.2022_
* [simplify esmock.d.ts file,](https://github.com/iambumblehead/esmock/pull/163) reducing documentation and using shorter param names
* [simplify args normalization](https://github.com/iambumblehead/esmock/pull/162) and drop support for opts.parent
* [simplify](https://github.com/iambumblehead/esmock/pull/159/files) [path-extraction](https://github.com/iambumblehead/esmock/pull/161) to use one regexp rather than four
* [use newer resolvewithplus](https://github.com/iambumblehead/resolvewithplus/releases/tag/v1.0.2) with better fileurl support
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "esmock",
"type": "module",
"version": "2.0.2",
"version": "2.0.3",
"license": "ISC",
"readmeFilename": "README.md",
"description": "provides native ESM import mocking for unit tests",
Expand Down
77 changes: 33 additions & 44 deletions src/esmock.d.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,71 @@
type mocks = Record<string, any>

type opts = {
strict?: boolean | undefined,
purge?: boolean | undefined,
isModuleNotFoundError?: boolean | undefined
}

/**
* Mocks imports for the module specified by {@link modulePath}.
*
* By default, mock definitions are merged with the original module definitions.
* To disable the default behaviour, Use esmock.strict.
* To avoid the default behaviour, use esmock.strict.
*
* @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.
* If not specified, inferred via the stack. Useful with source maps.
* @param defs A mapping of import specifiers to mock definitions.
* @param gdefs A mapping of import specifiers to mock definitions, applied globally.
* @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 function esmock(modulePath: string, parent: string, defs?: mocks, gdefs?: mocks, opt?: opts): any
declare function esmock(modulePath: string, defs?: mocks, gdefs?: mocks, opt?: opts): any

declare namespace esmock {
interface Options {
strict?: boolean | undefined;
purge?: boolean | undefined;
isModuleNotFoundError?: boolean | undefined;
parent?: string | undefined;
}

/**
* Mocks imports for the module specified by {@link modulePath}.
*
* This "strict" variant gives un-modified mock definitions that are not
* merged with original module definitions.
* The "strict" variant replaces original module definitions with mock definitions.
*
* @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.
* If not specified, inferred via the stack. Useful with source maps.
* @param defs A mapping of import specifiers to mock definitions.
* @param gdefs A mapping of import specifiers to mock definitions, applied globally.
* @param opt
* @returns The result of importing {@link modulePath}, similar to `import(modulePath)`.
*/
function strict(modulePath: string, parent: string, mockDefs?: Record<string, any>, globalDefs?: Record<string, any>, opt?: esmock.Options): any;
function strict(modulePath: string, mockDefs?: Record<string, any>, globalDefs?: Record<string, any>, opt?: esmock.Options): any;
function strict(modulePath: string, parent: string, defs?: mocks, gdefs?: mocks, opt?: opts): any
function strict(modulePath: string, defs?: mocks, gdefs?: mocks, opt?: opts): any
export namespace strict {
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;
function p(modulePath: string, parent: string, defs?: mocks, gdefs?: mocks, opt?: opts): any
function p(modulePath: string, defs?: mocks, gdefs?: mocks, opt?: opts): any
}

/**
* Mocks dynamic imports for the module specified by {@link modulePath}.
* Uses caching to support `await import()` inside the mocked import tree.
*
* 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.
* If not specified, inferred via the stack. Useful with source maps.
* @param defs A mapping of import specifiers to mock definitions.
* @param gdefs A mapping of import specifiers to mock definitions, applied globally.
* @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;
function p(modulePath: string, parent: string, defs?: mocks, gdefs?: mocks, opt?: opts): any
function p(modulePath: string, defs?: mocks, gdefs?: mocks, opt?: opts): any

/**
* Unregisters a dynamic mock created by {@link esmock.p}.
* Removes caching created by {@link esmock.p}.
*
* @param mockModule A module object that was previously returned by {@link esmock.p}.
* @param mockModule A module object returned from {@link esmock.p}.
*/
function purge(mockModule: any): void;
function purge(mockModule: any): void
}

export { esmock as default, esmock as strict };
export { esmock as default, esmock as strict }