Skip to content

Commit b8f1d43

Browse files
Implement didNotReceive and bug fixes (#78)
* upgrade dependencies * audit fix * implement didNotReceive * fix incorrect state checkings * fix tests
1 parent 12a9ec4 commit b8f1d43

File tree

8 files changed

+1334
-3648
lines changed

8 files changed

+1334
-3648
lines changed

package-lock.json

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

package.json

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,24 @@
1515
"author": "",
1616
"license": "ISC",
1717
"devDependencies": {
18+
"@ava/typescript": "^1.1.0",
1819
"@types/node": "latest",
19-
"ava": "^1.2.0",
20-
"ts-node": "^7.0.0",
20+
"ava": "^3.3.0",
2121
"typescript": "^3.0.0"
2222
},
2323
"ava": {
24-
"files": [
25-
"./dist/spec/**/*.js"
26-
],
27-
"sources": [
28-
"./src/**/*.ts",
29-
"./spec/**/*.test.ts",
30-
"./dist/src/**/*.js",
31-
"./dist/spec/**/*.test.js"
32-
],
24+
"typescript": {
25+
"rewritePaths": {
26+
"/": "dist/"
27+
},
28+
"extensions": [
29+
"ts"
30+
]
31+
},
3332
"cache": false,
3433
"concurrency": 1,
3534
"failFast": true,
36-
"failWithoutAssertions": true,
37-
"compileEnhancements": false,
38-
"extensions": [
39-
"ts"
40-
],
41-
"require": [
42-
"ts-node/register"
43-
]
44-
}
35+
"failWithoutAssertions": true
36+
},
37+
"dependencies": {}
4538
}

spec/index.test.ts

Lines changed: 76 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,24 @@ export class Example {
1818
return 1337;
1919
}
2020

21-
set v(x: string|null|undefined) {
21+
set v(x: string | null | undefined) {
2222
}
2323

24-
received(stuff: number|string) {
24+
received(stuff: number | string) {
2525

2626
}
2727

2828
returnPromise() {
2929
return Promise.resolve(new Dummy());
3030
}
3131

32-
foo(): string|undefined|null {
32+
foo(): string | undefined | null {
3333
return 'stuff';
34-
}
35-
36-
bar (a: number, b?: number): number{
37-
return a + b || 0
38-
}
34+
}
35+
36+
bar(a: number, b?: number): number {
37+
return a + b || 0
38+
}
3939
}
4040

4141
let instance: Example;
@@ -46,20 +46,15 @@ function initialize() {
4646
substitute = Substitute.for<Example>();
4747
};
4848

49-
test('can call received twice', t => {
49+
test('can call received twice', t => {
5050
initialize();
5151

5252
substitute.c('blah', 'fuzz');
53+
const expectedMessage = 'Expected 1337 calls to the method c with arguments [\'foo\', \'bar\'], but received none of such calls.\nAll calls received to method c:\n-> call with arguments [\'blah\', \'fuzz\']'
54+
t.throws(() => substitute.received(1337).c('foo', 'bar'), { message: expectedMessage });
5355

54-
t.throws(() => substitute.received(1337).c('foo', 'bar'),
55-
`Expected 1337 calls to the method c with arguments ['foo', 'bar'], but received none of such calls.
56-
All calls received to method c:
57-
-> call with arguments ['blah', 'fuzz']`);
58-
59-
t.throws(() => substitute.received(2117).c('foo', 'bar'),
60-
`Expected 2117 calls to the method c with arguments ['foo', 'bar'], but received none of such calls.
61-
All calls received to method c:
62-
-> call with arguments ['blah', 'fuzz']`);
56+
const expectedMessage2 = 'Expected 2117 calls to the method c with arguments [\'foo\', \'bar\'], but received none of such calls.\nAll calls received to method c:\n-> call with arguments [\'blah\', \'fuzz\']'
57+
t.throws(() => substitute.received(2117).c('foo', 'bar'), { message: expectedMessage2 });
6358
});
6459

6560
test('class string field get returns', t => {
@@ -75,7 +70,7 @@ test('class string field get returns', t => {
7570

7671
test('class with method called "received" can be used for call count verification when proxies are suspended', t => {
7772
initialize();
78-
73+
7974
Substitute.disableFor(substitute).received(2);
8075

8176
t.throws(() => substitute.received(2).received(2));
@@ -84,7 +79,7 @@ test('class with method called "received" can be used for call count verificatio
8479

8580
test('class with method called "received" can be used for call count verification', t => {
8681
initialize();
87-
82+
8883
Substitute.disableFor(substitute).received('foo');
8984

9085
t.notThrows(() => substitute.received(1).received('foo'));
@@ -93,15 +88,15 @@ test('class with method called "received" can be used for call count verificatio
9388

9489
test('partial mocks using function mimicks with all args', t => {
9590
initialize();
96-
91+
9792
substitute.c(Arg.all()).mimicks(instance.c);
9893

9994
t.deepEqual(substitute.c('a', 'b'), 'hello a world (b)');
10095
});
10196

10297
test('class string field get received', t => {
10398
initialize();
104-
99+
105100
void substitute.a;
106101
void substitute.a;
107102
void substitute.a;
@@ -114,13 +109,13 @@ test('class string field get received', t => {
114109

115110
test('class string field set received', t => {
116111
initialize();
117-
112+
118113
substitute.v = undefined;
119114
substitute.v = null;
120115
substitute.v = 'hello';
121116
substitute.v = 'hello';
122117
substitute.v = 'world';
123-
118+
124119
t.notThrows(() => substitute.received().v = 'hello');
125120
t.notThrows(() => substitute.received(5).v = Arg.any());
126121
t.notThrows(() => substitute.received().v = Arg.any());
@@ -135,9 +130,9 @@ test('class string field set received', t => {
135130

136131
test('class method returns with placeholder args', t => {
137132
initialize();
138-
133+
139134
substitute.c(Arg.any(), "there").returns("blah", "haha");
140-
135+
141136
t.is(substitute.c("hi", "there"), 'blah');
142137
t.is(substitute.c("his", "there"), 'haha');
143138
t.is<any>(substitute.c("his", "there"), void 0);
@@ -146,15 +141,15 @@ test('class method returns with placeholder args', t => {
146141

147142
test('partial mocks using function mimicks with specific args', t => {
148143
initialize();
149-
144+
150145
substitute.c('a', 'b').mimicks(instance.c);
151146

152147
t.is(substitute.c('a', 'b'), 'hello a world (b)');
153148
});
154149

155150
test('class method returns with specific args', t => {
156151
initialize();
157-
152+
158153
substitute.c("hi", "there").returns("blah", "haha");
159154

160155
t.is(substitute.c("hi", "there"), 'blah');
@@ -165,32 +160,32 @@ test('class method returns with specific args', t => {
165160

166161
test('returning other fake from promise works', async t => {
167162
initialize();
168-
163+
169164
const otherSubstitute = Substitute.for<Dummy>();
170165
substitute.returnPromise().returns(Promise.resolve(otherSubstitute));
171-
t.is(otherSubstitute, await substitute.returnPromise());
166+
t.is(otherSubstitute, await substitute.returnPromise());
172167
});
173168

174169
test('returning resolved promises works', async t => {
175170
initialize();
176-
171+
177172
substitute.returnPromise().returns(Promise.resolve(1338));
178173

179174
t.is(1338, await substitute.returnPromise());
180175
});
181176

182177
test('class void returns', t => {
183178
initialize();
184-
179+
185180
substitute.foo().returns(void 0, null);
186181

187182
t.is(substitute.foo(), void 0);
188183
t.is(substitute.foo(), null);
189-
});
184+
});
190185

191186
test('class method received', t => {
192187
initialize();
193-
188+
194189
void substitute.c("hi", "there");
195190
void substitute.c("hi", "the1re");
196191
void substitute.c("hi", "there");
@@ -201,92 +196,92 @@ test('class method received', t => {
201196
t.notThrows(() => substitute.received(1).c('hi', 'the1re'));
202197
t.notThrows(() => substitute.received().c('hi', 'there'));
203198

204-
t.throws(() => substitute.received(7).c('hi', 'there'),
205-
`Expected 7 calls to the method c with arguments ['hi', 'there'], but received 4 of such calls.
206-
All calls received to method c:
207-
-> call with arguments ['hi', 'there']
208-
-> call with arguments ['hi', 'the1re']
209-
-> call with arguments ['hi', 'there']
210-
-> call with arguments ['hi', 'there']
211-
-> call with arguments ['hi', 'there']`);
199+
const expectedMessage = 'Expected 7 calls to the method c with arguments [\'hi\', \'there\'], but received 4 of such calls.\n' +
200+
'All calls received to method c:\n' +
201+
'-> call with arguments [\'hi\', \'there\']\n' +
202+
'-> call with arguments [\'hi\', \'the1re\']\n' +
203+
'-> call with arguments [\'hi\', \'there\']\n' +
204+
'-> call with arguments [\'hi\', \'there\']\n' +
205+
'-> call with arguments [\'hi\', \'there\']'
206+
t.throws(() => { substitute.received(7).c('hi', 'there') }, { message: expectedMessage });
212207
});
213208

214209
test('received call matches after partial mocks using property instance mimicks', t => {
215210
initialize();
216-
211+
217212
substitute.d.mimicks(() => instance.d);
218213
substitute.c('lala', 'bar');
219214

220215
substitute.received(1).c('lala', 'bar');
221216
substitute.received(1).c('lala', 'bar');
222217

223218
t.notThrows(() => substitute.received(1).c('lala', 'bar'));
224-
t.throws(() => substitute.received(2).c('lala', 'bar'),
225-
`Expected 2 calls to the method c with arguments ['lala', 'bar'], but received 1 of such call.
226-
All calls received to method c:
227-
-> call with arguments ['lala', 'bar']`);
228-
219+
const expectedMessage = 'Expected 2 calls to the method c with arguments [\'lala\', \'bar\'], but received 1 of such call.\n' +
220+
'All calls received to method c:\n' +
221+
'-> call with arguments [\'lala\', \'bar\']'
222+
t.throws(() => substitute.received(2).c('lala', 'bar'), { message: expectedMessage });
223+
229224
t.deepEqual(substitute.d, 1337);
230225
});
231226

232227
test('partial mocks using property instance mimicks', t => {
233228
initialize();
234-
229+
235230
substitute.d.mimicks(() => instance.d);
236231

237232
t.deepEqual(substitute.d, 1337);
238233
});
239234

240235
test('verifying with more arguments fails', t => {
241-
initialize()
242-
substitute.bar(1)
243-
substitute.received().bar(1)
244-
t.throws(() => substitute.received().bar(1, 2))
236+
initialize()
237+
substitute.bar(1)
238+
substitute.received().bar(1)
239+
t.throws(() => substitute.received().bar(1, 2))
245240
})
246241

247242
test('verifying with less arguments fails', t => {
248-
initialize()
249-
substitute.bar(1, 2)
250-
substitute.received().bar(1, 2)
251-
t.throws(() => substitute.received().bar(1))
243+
initialize()
244+
substitute.bar(1, 2)
245+
substitute.received().bar(1, 2)
246+
t.throws(() => substitute.received().bar(1))
252247
})
253248

254249
test('return with more arguments is not matched fails', t => {
255-
initialize()
256-
substitute.bar(1, 2).returns(3)
257-
t.is(3, substitute.bar(1, 2))
258-
t.is('function', typeof(substitute.bar(1)))
250+
initialize()
251+
substitute.bar(1, 2).returns(3)
252+
t.is(3, substitute.bar(1, 2))
253+
t.is('function', typeof (substitute.bar(1)))
259254
})
260255

261256
test('return with less arguments is not matched', t => {
262-
initialize()
263-
substitute.bar(1).returns(3)
264-
t.is(3, substitute.bar(1))
265-
t.is('function', typeof(substitute.bar(1, 2).toString))
257+
initialize()
258+
substitute.bar(1).returns(3)
259+
t.is(3, substitute.bar(1))
260+
t.is('function', typeof (substitute.bar(1, 2).toString))
266261
})
267262

268263
test('can stub multiple primitive return values', t => {
269-
initialize()
270-
substitute.bar(1).returns(2)
271-
substitute.bar(2).returns(3)
272-
t.is(2, substitute.bar(1))
273-
t.is(3, substitute.bar(2))
264+
initialize()
265+
substitute.bar(1).returns(2)
266+
substitute.bar(2).returns(3)
267+
t.is(2, substitute.bar(1))
268+
t.is(3, substitute.bar(2))
274269
})
275270

276271
test('can stub multiple Arg values', t => {
277-
initialize()
278-
substitute.bar(Arg.is(x => x % 2 === 0)).returns(1)
279-
substitute.bar(Arg.is(x => x % 2 !== 0)).returns(2)
280-
t.is(1, substitute.bar(4))
281-
t.is(2, substitute.bar(5))
272+
initialize()
273+
substitute.bar(Arg.is(x => x % 2 === 0)).returns(1)
274+
substitute.bar(Arg.is(x => x % 2 !== 0)).returns(2)
275+
t.is(1, substitute.bar(4))
276+
t.is(2, substitute.bar(5))
282277
})
283278

284279

285280
test.skip('can stub primitive & Arg values', t => {
286-
initialize()
287-
substitute.bar(1).returns(2)
288-
substitute.bar(Arg.any()).returns(3) // throws 'substitute.bar(...).returns is not a function'
289-
t.is(5, substitute.bar(2))
290-
t.is(2, substitute.bar(1))
291-
t.is(3, substitute.bar(2))
281+
initialize()
282+
substitute.bar(1).returns(2)
283+
substitute.bar(Arg.any()).returns(3) // throws 'substitute.bar(...).returns is not a function'
284+
t.is(5, substitute.bar(2))
285+
t.is(2, substitute.bar(1))
286+
t.is(3, substitute.bar(2))
292287
})

spec/issues/51.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ test('issue 51 - All functions shares the same state', async t => {
1515
try {
1616
calculator.received().divide(1, 2);
1717
} catch (e) {
18-
t.regex(e.toString(), /Error: there are no mock for property: divide/);
18+
t.regex(e.toString(), /Error: there is no mock for property: divide/);
1919
}
2020
});

0 commit comments

Comments
 (0)