-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix!: recreate mock on reset() (#76)
* fix: unify the behavior for direct behavior functions and onAnyCommand calls * fix: create new mock instance on reset() call This prevents mock behaviors breaking subsequent behaviors between the reset() calls due to Sinon.JS bug #1572. * fix!: remove resetBehavior() method The resetBehavior() method, calling Sinon.JS stub.resetBehavior(), cannot be relied upon because of Sinon.JS bug #1572.
- Loading branch information
1 parent
e3b3a1d
commit 9e1a873
Showing
5 changed files
with
83 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import {AwsClientStub, mockClient} from '../src'; | ||
import {PublishCommand, SNSClient} from '@aws-sdk/client-sns'; | ||
import {publishCmd1, publishCmd3, topicArn} from './fixtures'; | ||
|
||
let snsMock: AwsClientStub<SNSClient>; | ||
|
||
beforeEach(() => { | ||
snsMock = mockClient(SNSClient); | ||
}); | ||
|
||
afterEach(() => { | ||
snsMock.restore(); | ||
}); | ||
|
||
/** | ||
* Sinon.JS has a bug with a sinon.match breaking subsequent mocks in some scenarios, | ||
* including leaking the mock behaviors between the stub.reset() calls. | ||
* See: https://github.com/m-radzikowski/aws-sdk-client-mock/issues/67 and https://github.com/sinonjs/sinon/issues/1572 | ||
*/ | ||
describe('issue 67 - breaking subsequent mocks', () => { | ||
const sns = new SNSClient({}); | ||
|
||
/** | ||
* This corresponds to the pattern with mock.reset() and mock.onAnyCommand().rejects() | ||
* being called in the beforeEach() block and then individual mock behaviors being set in test functions. | ||
*/ | ||
test('resetting mock does not break subsequent mocks', async () => { | ||
snsMock.onAnyCommand().rejects('any command error'); | ||
snsMock.on(PublishCommand).rejects('publish error'); | ||
|
||
snsMock.reset(); | ||
snsMock.onAnyCommand().rejects('any command error'); | ||
|
||
const publish = sns.send(publishCmd1); | ||
|
||
await expect(publish).rejects.toThrow('any command error'); | ||
}); | ||
|
||
/** | ||
* Make sure the main behavior described in the Sinon.JS bug, with match.any breaking subsequent stubs, | ||
* does not happen. | ||
*/ | ||
test('onAnyCommand does not brake other mocks', async () => { | ||
snsMock.onAnyCommand().rejects('any command error'); | ||
snsMock.onAnyCommand({TopicArn: topicArn}).rejects('any command first topic error'); | ||
|
||
const publish1 = sns.send(publishCmd1); | ||
const publish2 = sns.send(publishCmd3); | ||
|
||
await expect(publish1).rejects.toThrow('any command first topic error'); | ||
await expect(publish2).rejects.toThrow('any command error'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters