Skip to content

Commit a565d8f

Browse files
authored
Merge pull request #437 from mohaalak/types_for_stubbing
update td.when types
2 parents 8ed503f + 4de0a6c commit a565d8f

File tree

2 files changed

+63
-23
lines changed

2 files changed

+63
-23
lines changed

index.d.ts

+35-16
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
// types and interfaces
33
// ----------------------------------------------------------------------------
44

5-
export type DoubledObject < T > = T
5+
export type DoubledObject<T> = T;
66

7-
export type DoubledObjectWithKey < T extends string > = { [K in T] : any }
7+
export type DoubledObjectWithKey<T extends string> = { [K in T]: any };
88

9-
export type TestDouble < T > = T
9+
export type TestDouble<T> = T;
1010

11-
export type TestDoubleConstructor < T > = Constructor<T>
11+
export type TestDoubleConstructor<T> = Constructor<T>;
1212

1313
interface Call {
1414
context: {};
1515
args: any[];
1616
}
1717

1818
interface Constructor<T> {
19-
new (...args: any[]): T
19+
new (...args: any[]): T;
2020
}
2121

2222
export interface Captor {
@@ -49,15 +49,21 @@ export interface Matchers {
4949
create(config: MatcherConfig): any;
5050
}
5151

52-
export const matchers: Matchers
52+
export const matchers: Matchers;
5353

54-
export interface Stubber {
55-
thenReturn<T>(...args: any[]): TestDouble<T>;
54+
export interface Stubber<D, R = D extends object ? Partial<D> : D> {
55+
thenReturn<T>(first: R, ...args: Array<R>): TestDouble<T>;
5656
thenDo<T>(f: Function): TestDouble<T>;
5757
thenThrow<T>(e: Error): TestDouble<T>;
58-
thenResolve<T>(...args: any[]): TestDouble<T>;
58+
thenResolve<T>(first: R, ...args: Array<R>): TestDouble<T>;
59+
thenReject<T>(e: Error): TestDouble<T>;
60+
thenCallback<T>(error: any, data: any): TestDouble<T>;
61+
}
62+
63+
export interface PromiseStubber<P, R = P extends object ? Partial<P> : P> {
64+
thenResolve<T>(first: R, ...args: Array<R>): TestDouble<T>;
65+
thenDo<T>(f: Function): TestDouble<T>;
5966
thenReject<T>(e: Error): TestDouble<T>;
60-
thenCallback<T>(...args: any[]): TestDouble<T>;
6167
}
6268

6369
export interface TestdoubleConfig {
@@ -72,6 +78,14 @@ export interface VerificationConfig {
7278
cloneArgs?: boolean;
7379
}
7480

81+
export interface WhenConfig {
82+
ignoreExtraArgs?: boolean;
83+
times?: number;
84+
cloneArgs?: boolean;
85+
defer?: boolean;
86+
delay?: number;
87+
}
88+
7589
//
7690
// general
7791
// ----------------------------------------------------------------------------
@@ -117,7 +131,9 @@ export function explain<T>(f: TestDouble<T>): Explanation;
117131
* @param {{ new (...args: any[]): T }} constructor
118132
* @returns {DoubledObject<T>}
119133
*/
120-
export function constructor<T>(constructor: Constructor<T>): TestDoubleConstructor<T>;
134+
export function constructor<T>(
135+
constructor: Constructor<T>
136+
): TestDoubleConstructor<T>;
121137

122138
//
123139
// fake: functions
@@ -140,8 +156,8 @@ declare function functionDouble(name?: string): TestDouble<Function>;
140156
*/
141157
declare function functionDouble<T>(name?: T): TestDouble<T>;
142158

143-
export { functionDouble as function }
144-
export { functionDouble as func }
159+
export { functionDouble as function };
160+
export { functionDouble as func };
145161

146162
//
147163
// fake: objects
@@ -207,7 +223,10 @@ export function object<T>(object: T): DoubledObject<T>;
207223
* @param {string} [name]
208224
* @returns {TestDoubleConstructor<T>}
209225
*/
210-
export function imitate<T>(constructor: Constructor<T>, name?: string): TestDoubleConstructor<T>;
226+
export function imitate<T>(
227+
constructor: Constructor<T>,
228+
name?: string
229+
): TestDoubleConstructor<T>;
211230

212231
/**
213232
* Create a fake object or function.
@@ -262,8 +281,8 @@ export function replace(path: {}, property: string, f?: any): any;
262281
* @param {...any[]} args
263282
* @returns {Stubber}
264283
*/
265-
export function when(...args: any[]): Stubber;
266-
284+
export function when<P>(f: Promise<P>, config?: WhenConfig): PromiseStubber<P>;
285+
export function when<D>(f: D, config?: WhenConfig): Stubber<D>;
267286
/**
268287
* Verify a specific function call to a stubbed function.
269288
*

test/safe/typescript-typings.test.ts

+28-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ class Dog {
66
}
77

88
class Cat {
9-
meow (): string { return 'meow! meow!' }
9+
meow (): string {
10+
return 'meow! meow!'
11+
}
1012
}
1113

1214
function sum (first: number, second: number): number {
@@ -18,19 +20,36 @@ export = {
1820
const dog = td.constructor(Dog)
1921
td.when(dog.prototype.bark()).thenReturn('woof!')
2022

21-
const bird = td.object({ fly: function () {} })
23+
const bird = td.object({
24+
fly: function (): string {
25+
return 'swoosh'
26+
}
27+
})
2228
td.when(bird.fly()).thenReturn('swoosh!')
2329

30+
const fish = td.object<{
31+
swim(): { speed: number; direction: number };
32+
}>()
33+
td.when(fish.swim()).thenReturn({ speed: 100 })
34+
2435
const kitty = td.object(['scratch', 'meow'])
2536
td.when(kitty.scratch()).thenReturn('scratch!')
2637
td.when(kitty.meow()).thenReturn('meow!')
2738

28-
if (eval('typeof Proxy') !== 'undefined') { // eslint-disable-line
29-
class Bear { sleep () {} }
39+
// eslint-disable-next-line
40+
if (eval("typeof Proxy") !== "undefined") {
41+
class Bear {
42+
sleep (): string {
43+
return 'test'
44+
}
45+
}
3046

3147
const FakeBear = td.constructor<Bear>(Bear)
3248

33-
assert.strictEqual(td.explain(FakeBear.prototype.sleep).isTestDouble, true)
49+
assert.strictEqual(
50+
td.explain(FakeBear.prototype.sleep).isTestDouble,
51+
true
52+
)
3453

3554
const bear = td.object<Bear>('A bear')
3655

@@ -51,7 +70,9 @@ export = {
5170
const f = td.function()
5271
td.when(f(10)).thenReturn(10, 11)
5372
td.when(f(1)).thenThrow(new Error('ok'))
54-
td.when(f(td.matchers.isA(String))).thenDo(function (s: string) { return s })
73+
td.when(f(td.matchers.isA(String))).thenDo(function (s: string) {
74+
return s
75+
})
5576
td.when(f(td.matchers.not(true))).thenResolve('value1', 'value2')
5677
td.when(f(td.matchers.not(false))).thenReject(new Error('rejected'))
5778

@@ -78,4 +99,4 @@ export = {
7899
'This test double has 5 stubbings and 1 invocations.'
79100
)
80101
}
81-
}
102+
};

0 commit comments

Comments
 (0)