Skip to content

Commit b348368

Browse files
refactor promise type logic (#88)
1 parent e25bf9d commit b348368

File tree

2 files changed

+15
-32
lines changed

2 files changed

+15
-32
lines changed

package-lock.json

Lines changed: 6 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Transformations.ts

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
import { AllArguments } from "./Arguments";
22

3-
export type NoArgumentFunctionSubstitute<TReturnType> =
4-
TReturnType extends Promise<infer U> ?
5-
(() => (TReturnType & NoArgumentMockObjectPromise<TReturnType>)) :
6-
(() => (TReturnType & NoArgumentMockObjectMixin<TReturnType>))
3+
export type NoArgumentFunctionSubstitute<TReturnType> = (() => (TReturnType & NoArgumentMockObjectMixin<TReturnType>))
74

85
export type FunctionSubstitute<TArguments extends any[], TReturnType> =
9-
TReturnType extends Promise<infer U> ?
10-
((...args: TArguments) => (TReturnType & MockObjectPromise<TArguments, TReturnType>)) &
11-
((allArguments: AllArguments) => (TReturnType & MockObjectPromise<TArguments, TReturnType>)) :
12-
((...args: TArguments) => (TReturnType & MockObjectMixin<TArguments, TReturnType>)) &
13-
((allArguments: AllArguments) => (TReturnType & MockObjectMixin<TArguments, TReturnType>))
6+
((...args: TArguments) => (TReturnType & MockObjectMixin<TArguments, TReturnType>)) &
7+
((allArguments: AllArguments) => (TReturnType & MockObjectMixin<TArguments, TReturnType>))
148

159
export type PropertySubstitute<TReturnType> = (TReturnType & Partial<NoArgumentMockObjectMixin<TReturnType>>);
1610

17-
type Unpacked<T> =
18-
T extends Promise<infer U> ? U :
19-
T;
11+
type MockObjectPromise<TReturnType> = TReturnType extends Promise<infer U> ? {
12+
resolves: (...args: U[]) => void;
13+
rejects: (exception: any) => void;
14+
} : {}
2015

21-
type BaseMockObjectMixin<TReturnType> = {
16+
type BaseMockObjectMixin<TReturnType> = MockObjectPromise<TReturnType> & {
2217
returns: (...args: TReturnType[]) => void;
23-
throws: (exception: any) => void;
18+
throws: (exception: any) => never;
2419
}
2520

2621
type NoArgumentMockObjectMixin<TReturnType> = BaseMockObjectMixin<TReturnType> & {
@@ -31,16 +26,6 @@ type MockObjectMixin<TArguments extends any[], TReturnType> = BaseMockObjectMixi
3126
mimicks: (func: (...args: TArguments) => TReturnType) => void;
3227
}
3328

34-
type NoArgumentMockObjectPromise<TReturnType> = NoArgumentMockObjectMixin<TReturnType> & {
35-
resolves: (...args: Unpacked<TReturnType>[]) => void;
36-
rejects: (exception: any) => void;
37-
}
38-
39-
type MockObjectPromise<TArguments extends any[], TReturnType> = MockObjectMixin<TArguments, TReturnType> & {
40-
resolves: (...args: Unpacked<TReturnType>[]) => void;
41-
rejects: (exception: any) => void;
42-
}
43-
4429
export type ObjectSubstitute<T extends Object, K extends Object = T> = ObjectSubstituteTransformation<T> & {
4530
received(amount?: number): TerminatingObject<K>;
4631
didNotReceive(): TerminatingObject<K>;

0 commit comments

Comments
 (0)