Skip to content

Commit 65d53a7

Browse files
authored
fix!: Correct expect.assertions() count on CommandWith commands (#209)
1 parent 6debb61 commit 65d53a7

File tree

2 files changed

+43
-24
lines changed

2 files changed

+43
-24
lines changed

packages/aws-sdk-client-mock-jest/src/jestMatchers.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -332,16 +332,7 @@ const baseMatchers: { [P in keyof AwsSdkJestMockBaseMatchers<unknown>]: MatcherF
332332
check: ({commandCalls}) => {
333333
const matchCount = commandCalls
334334
.map(call => call.args[0].input) // eslint-disable-line @typescript-eslint/no-unsafe-return
335-
.map(received => {
336-
try {
337-
expect(received).toEqual(
338-
expect.objectContaining(input),
339-
);
340-
return true;
341-
} catch (e) {
342-
return false;
343-
}
344-
})
335+
.map(received => this.equals(received, expect.objectContaining(input)))
345336
.reduce((acc, val) => acc + Number(val), 0);
346337

347338
return {pass: matchCount > 0, data: {matchCount}};
@@ -382,13 +373,7 @@ const baseMatchers: { [P in keyof AwsSdkJestMockBaseMatchers<unknown>]: MatcherF
382373

383374
let pass = false;
384375
if (received instanceof command) {
385-
try {
386-
expect(received.input).toEqual(
387-
expect.objectContaining(input),
388-
);
389-
pass = true;
390-
} catch (e) { // eslint-disable-line no-empty
391-
}
376+
pass = this.equals(received.input, expect.objectContaining(input));
392377
}
393378

394379
return {
@@ -443,13 +428,7 @@ const baseMatchers: { [P in keyof AwsSdkJestMockBaseMatchers<unknown>]: MatcherF
443428

444429
let pass = false;
445430
if (received instanceof command) {
446-
try {
447-
expect(received.input).toEqual(
448-
expect.objectContaining(input),
449-
);
450-
pass = true;
451-
} catch (e) { // eslint-disable-line no-empty
452-
}
431+
pass = this.equals(received.input, expect.objectContaining(input));
453432
}
454433

455434
return {

packages/aws-sdk-client-mock-jest/test/jestMatchers.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ afterEach(() => {
1515

1616
describe('toHaveReceivedCommand', () => {
1717
it('passes on receiving Command', async () => {
18+
expect.assertions(2);
19+
1820
const sns = new SNSClient({});
1921
await sns.send(publishCmd1);
2022

@@ -29,6 +31,8 @@ SNSClient received <green>"PublishCommand"</color> <red>0</color> times"
2931
});
3032

3133
it('fails on receiving Command with not', async () => {
34+
expect.assertions(2);
35+
3236
const sns = new SNSClient({});
3337
await sns.send(publishCmd1);
3438

@@ -42,6 +46,9 @@ Calls:
4246
});
4347

4448
it('fails on more arguments', async () => {
49+
// We only expect 1 assertion here due to expected "internal error" in toHaveReceivedCommand
50+
expect.assertions(1);
51+
4552
const sns = new SNSClient({});
4653
await sns.send(publishCmd1);
4754

@@ -53,6 +60,8 @@ Calls:
5360

5461
describe('toHaveReceivedCommandTimes', () => {
5562
it('passes on receiving Command twice', async () => {
63+
expect.assertions(2);
64+
5665
const sns = new SNSClient({});
5766
await sns.send(publishCmd1);
5867
await sns.send(publishCmd2);
@@ -74,6 +83,8 @@ Calls:
7483
});
7584

7685
it('fails on receiving Command twice with not', async () => {
86+
expect.assertions(2);
87+
7788
const sns = new SNSClient({});
7889
await sns.send(publishCmd1);
7990
await sns.send(publishCmd1);
@@ -91,6 +102,8 @@ Calls:
91102

92103
describe('toHaveReceivedCommandWith', () => {
93104
it('passes on receiving Command with partial match', async () => {
105+
expect.assertions(2);
106+
94107
const sns = new SNSClient({});
95108
await sns.send(publishCmd1);
96109
await sns.send(publishCmd2);
@@ -99,6 +112,8 @@ describe('toHaveReceivedCommandWith', () => {
99112
});
100113

101114
it('fails on not receiving Command', async () => {
115+
expect.assertions(2);
116+
102117
const sns = new SNSClient({});
103118
await sns.send(publishCmd1);
104119

@@ -112,6 +127,8 @@ Calls:
112127
});
113128

114129
it('fails on receiving Command with partial match with not', async () => {
130+
expect.assertions(2);
131+
115132
const sns = new SNSClient({});
116133
await sns.send(publishCmd1);
117134

@@ -125,6 +142,8 @@ Calls:
125142
});
126143

127144
it('passes on match with asymmetric matcher', async () => {
145+
expect.assertions(2);
146+
128147
const sns = new SNSClient({});
129148
await sns.send(publishCmd1);
130149

@@ -134,6 +153,8 @@ Calls:
134153
});
135154

136155
it('fails on unmatch with asymmetric matcher', async () => {
156+
expect.assertions(2);
157+
137158
const sns = new SNSClient({});
138159
await sns.send(publishCmd1);
139160

@@ -151,6 +172,8 @@ Calls:
151172

152173
describe('toHaveReceivedNthCommandWith', () => {
153174
it('passes on receiving second Command with partial match', async () => {
175+
expect.assertions(2);
176+
154177
const sns = new SNSClient({});
155178
await sns.send(publishCmd1);
156179
await sns.send(publishCmd2);
@@ -159,6 +182,8 @@ describe('toHaveReceivedNthCommandWith', () => {
159182
});
160183

161184
it('fails on not receiving second Command', async () => {
185+
expect.assertions(2);
186+
162187
const sns = new SNSClient({});
163188
await sns.send(publishCmd1);
164189
await sns.send(publishCmd1);
@@ -182,6 +207,8 @@ Calls:
182207
});
183208

184209
it('fails on receiving second Command with not', async () => {
210+
expect.assertions(2);
211+
185212
const sns = new SNSClient({});
186213
await sns.send(publishCmd1);
187214
await sns.send(publishCmd2);
@@ -204,6 +231,8 @@ Calls:
204231
});
205232

206233
it('fails on receiving less Commands than the nth requested', async () => {
234+
expect.assertions(2);
235+
207236
const sns = new SNSClient({});
208237
await sns.send(publishCmd1);
209238

@@ -216,6 +245,8 @@ Calls:
216245
});
217246

218247
it('passes on match with asymmetric matcher', async () => {
248+
expect.assertions(2);
249+
219250
const sns = new SNSClient({});
220251
await sns.send(publishCmd1);
221252
await sns.send(publishCmd2);
@@ -226,6 +257,8 @@ Calls:
226257
});
227258

228259
it('fails on unmatch with asymmetric matcher', async () => {
260+
expect.assertions(2);
261+
229262
const sns = new SNSClient({});
230263
await sns.send(publishCmd1);
231264
await sns.send(publishCmd2);
@@ -253,6 +286,8 @@ Calls:
253286

254287
describe('toHaveReceivedNthSpecificCommandWith', () => {
255288
it('passes on receiving second Command with partial match', async () => {
289+
expect.assertions(2);
290+
256291
const sns = new SNSClient({});
257292
await sns.send(publishCmd1);
258293
await sns.send(subscribeCmd1);
@@ -262,6 +297,8 @@ describe('toHaveReceivedNthSpecificCommandWith', () => {
262297
});
263298

264299
it('fails on receiving less Commands than the nth expected', async () => {
300+
expect.assertions(2);
301+
265302
const sns = new SNSClient({});
266303
await sns.send(publishCmd1);
267304

@@ -280,13 +317,16 @@ describe('toHaveReceivedAnyCommand', () => {
280317
${publishCmd1}
281318
${subscribeCmd1}
282319
`('passes on receiving any command', async ({ command }: { command: AwsCommand<any, any> }) => {
320+
expect.assertions(2);
321+
283322
const sns = new SNSClient({});
284323
await sns.send(command); // eslint-disable-line @typescript-eslint/no-unsafe-argument
285324

286325
expect(() => expect(snsMock).toHaveReceivedAnyCommand()).not.toThrow();
287326
});
288327

289328
it('fails on not receiving any command', () => {
329+
expect.assertions(2);
290330
expect(() => expect(snsMock).toHaveReceivedAnyCommand()).toThrowErrorMatchingInlineSnapshot(`
291331
"Expected SNSClient to receive any command
292332
SNSClient received any command <red>0</color> times"

0 commit comments

Comments
 (0)