@userlike/joke
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [0.3.0](https://github.com/userlike/joke/compare/v0.2.0...v0.3.0) (2020-02-26) | ||
### Features | ||
* add mockSome for partial mocking of modules ([0e92c8a](https://github.com/userlike/joke/commit/0e92c8a285de8b0f1ff4fdc57c82e206f8d876fa)) | ||
# [0.2.0](https://github.com/userlike/joke/compare/v0.2.0-alpha.3...v0.2.0) (2020-02-26) | ||
@@ -8,0 +19,0 @@ |
/// <reference types="jest" /> | ||
/** | ||
* A better Mocked type that handles nested objects. | ||
* (Hopefully) A better Mocked type that handles nested objects. | ||
*/ | ||
@@ -8,4 +8,16 @@ declare type Mocked<T> = { | ||
} & T; | ||
/** | ||
* | ||
* @param _ Dynamic imported module | ||
*/ | ||
export declare function mock<M>(_: Promise<M>): Mocked<M>; | ||
export declare function mock<M, P extends Partial<M>>(_: Promise<M>, _impl: () => P): Mocked<P>; | ||
/** | ||
* | ||
* @param _impl Mock factory that returns a partial implementation. The returned object is merged | ||
* with auto-generated module mock using `Object.assign`. | ||
*/ | ||
export declare function mock<M>(_: Promise<M>, _impl: () => Partial<M>): Mocked<M>; | ||
export declare function mockSome<M, K extends keyof M>(_: Promise<M>, _impl: () => { | ||
[_K in K]: Mocked<M>[_K]; | ||
}): Omit<M, K> & Pick<Mocked<M>, K>; | ||
export {}; |
@@ -10,7 +10,27 @@ "use strict"; | ||
exports.mock = mock; | ||
exports.mockSome = mockSome; | ||
/** | ||
* A better Mocked type that handles nested objects. | ||
* (Hopefully) A better Mocked type that handles nested objects. | ||
*/ | ||
/** | ||
* | ||
* @param _ Dynamic imported module | ||
*/ | ||
/** | ||
* | ||
* @param _impl Mock factory that returns a partial implementation. The returned object is merged | ||
* with auto-generated module mock using `Object.assign`. | ||
*/ | ||
function mock(_, _impl = () => ({})) { | ||
return unsafeCoerce(mockSafetyNet()); | ||
} | ||
function mockSome(_, _impl) { | ||
return unsafeCoerce(mockSafetyNet()); | ||
} | ||
function mockSafetyNet() { | ||
const safetyObj = {}; | ||
@@ -27,3 +47,3 @@ const safetyProxy = new Proxy(safetyObj, { | ||
}); | ||
return unsafeCoerce(safetyProxy); | ||
return safetyProxy; | ||
} | ||
@@ -30,0 +50,0 @@ |
{ | ||
"name": "@userlike/joke", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Typesafe mock utility with minimal boilerplate for jest", | ||
@@ -31,3 +31,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "609debe6277eb6520bbf8a97416fa480eee36ec1" | ||
"gitHead": "41c15d8e23741495eb61d6aa0cc019f004df26c7" | ||
} |
/** | ||
* A better Mocked type that handles nested objects. | ||
* (Hopefully) A better Mocked type that handles nested objects. | ||
*/ | ||
@@ -16,7 +16,14 @@ type Mocked<T> = { | ||
/** | ||
* | ||
* @param _ Dynamic imported module | ||
*/ | ||
export function mock<M>(_: Promise<M>): Mocked<M>; | ||
export function mock<M, P extends Partial<M>>( | ||
_: Promise<M>, | ||
_impl: () => P | ||
): Mocked<P>; | ||
/** | ||
* | ||
* @param _impl Mock factory that returns a partial implementation. The returned object is merged | ||
* with auto-generated module mock using `Object.assign`. | ||
*/ | ||
export function mock<M>(_: Promise<M>, _impl: () => Partial<M>): Mocked<M>; | ||
export function mock<M>( | ||
@@ -26,2 +33,15 @@ _: Promise<M>, | ||
): Mocked<M> { | ||
return unsafeCoerce(mockSafetyNet()); | ||
} | ||
export function mockSome<M, K extends keyof M>( | ||
_: Promise<M>, | ||
_impl: () => { | ||
[_K in K]: Mocked<M>[_K]; | ||
} | ||
): Omit<M, K> & Pick<Mocked<M>, K> { | ||
return unsafeCoerce(mockSafetyNet()); | ||
} | ||
function mockSafetyNet(): unknown { | ||
const safetyObj = {}; | ||
@@ -37,3 +57,3 @@ const safetyProxy = new Proxy(safetyObj, { | ||
return unsafeCoerce(safetyProxy); | ||
return safetyProxy; | ||
} | ||
@@ -40,0 +60,0 @@ |
Sorry, the diff of this file is not supported yet
313067
130