-
Notifications
You must be signed in to change notification settings - Fork 110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v5.5.0 broke mocks with jest.fn #256
Comments
One way of addressing this could be to wrap cb() and check if it has been called.
Still has the issue of exceptions, though. |
Good catch, we didn't account for the case where the jest mock was passed an implementation of its own. I need some time to investigate how to best tackle this case while still providing the benefits of |
Also if used with a void method like https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/EC2.html#describeVpcs-property
|
aws-sdk-mock@5.5.0 broke this test, due to the callback function not being included in the arguments. See dwyl/aws-sdk-mock#256 for more details. As a (temporary?) measure, adjusting the test to match expectations.
Thanks for the feedback everyone, picking this up next week. |
Any updates? |
I've been looking over this, but I don't believe there's an easy way to fix the issues caused by this change. It's also rather invasive that the library automatically wraps, but even if we would move this wrapping behind a configuration flag, it would still cause issues when active. So at this point I would opt to rollback this change, what's your thought @nelsonic ? |
@thomaux I don't think we can unpublish the latest ( We could roll-back by publishing a |
@nelsonic like @JaroVDH mentioned, integrating the code like that would not handle exceptions.. I believe the culprit is line 126: Passing the callback there, like: Should correctly invoke the replacement function. I'll spend some time on a solution around this. Agreed that we don't want to (can't) unpublish. A temporary solution would be to move this mapping behind a config flag, and release that as 5.5.1. |
I have a strong preference for the longer term solution. Especially if the people affected (anyone using Jest) can help verify that it works. 🤞 |
It is not only Jest that is impacted, I'm using it with sinon and had to rewrite most of my mocked function |
@loopingz can you not just use the previous versions of the package instead of rewriting? 🤷🏼♂️ |
@nelsonic yes i did revert at first, then I was okay to try to accomadate your changes, so i started a branch to do so, but got into the empty parameters bug, so I kept the |
Gotcha. Thanks @dependabot 💭 |
Allright, based on the input of @JaroVDH and @loopingz (many thanks), I think I have been able to fix the issues reported above. The code can be found here: https://github.com/thomaux/aws-sdk-mock/blob/fix-256/index.js#L124 The current code will not handle this case (a replacement function that only defines the callback): const jestMock = jest.fn((cb) => cb(null, 'item'));
awsMock.mock('DynamoDB', 'getItem', jestMock); Do we want to support this? |
This release was an unintended breaking change. dwyl/aws-sdk-mock#256
I opened a PR for this, I went ahead and did add a clause to the code to also handle the following case: const jestMock = jest.fn((cb) => cb(null, 'item'));
awsMock.mock('DynamoDB', 'getItem', jestMock); |
This release was an unintended breaking change. dwyl/aws-sdk-mock#256
Back from vacation and ran our tests against 5.5.1, no more failures 👍 |
I preferred the 5.5.0 behavior since we are mocking with promise returning functions. We don't need, or expect, a callback function to be passed to replace. In fact, I was pleased to be able to replace every Is there some way to support both mocking styles? Maybe pass an extra parameter to |
@mjdickinson The 5.5.0 behaviour is still supported. The SDK methods are always called with a callback, even if you use 5.5.0 introduced this option, which in my opinion offers cleaner tests: // Given
const putMock = jest.fn();
mockAWS('DynamoDB.DocumentClient', 'put', putMock);
// When
await documentClient.put(item).promise();
// Then
expect(putMock).toHaveBeenCalledWith(item); |
Prior to 5.5.0 our tests were pretty clean:
5.5.0 allowed us to remove the I understand that 5.5.0 broke callback style tests. Indeed, it broke all of our tests too. I was just wondering if there is any interest in being able to control whether a callback is passed to the mock or not? We would certainly appreciate it the interest of cleaner promise style tests.
For the moment we're sticking with 5.5.0 because we prefer the tests without the extra |
Thanks for the details @mjdickinson, it was my understanding the behaviour you are seeking was still supported. I'll reproduce this example and see what's going wrong in 5.5.1. For the record, the whole reason I initially suggested the changes for 5.5.0 was to make it easier to test using Jest. I didn't want to manually have to wrap my Jest Mocks before passing them to the library. Just out of curiosity, does the following approach work (notice the change in how I defined the Jest mock): // Given
const listBuckets = jest.fn().mockResolvedValue({ Buckets: [] });
awsMock.mock('S3', 'listBuckets', listBuckets);
// When
await new aws.S3().listBuckets({}).promise();
// Then
expect(listBuckets).toHaveBeenCalledWith({}); |
No, it behaves the same. That's actually closer to how our unit tests are
written. We set default mocks on AWS service methods in shared test setup
and then override with specific implementation in individual tests.
…On Fri, Jan 7, 2022 at 1:40 AM Thomas Anciaux ***@***.***> wrote:
Thanks for the details @mjdickinson <https://github.com/mjdickinson>, it
was my understanding the behaviour you are seeking was still supported.
I'll reproduce this example and see what's going wrong in 5.5.1.
For the record, the whole reason I initially suggested the changes for
5.5.0 was to make it easier to test using Jest. I didn't want to manually
have to wrap my Jest Mocks before passing them to the library. In my
projects we're exclusively using promise based method calls, which is why I
missed the potential issues with callbacks and hence the reason why we
fixed it in 5.5.1.
Just out of curiosity, does the following approach work (notice the change
in how I defined the Jest mock):
// Givenconst listBuckets = jest.fn().mockResolvedValue({ Buckets: [] });awsMock.mock('S3', 'listBuckets', listBuckets);
// Whenawait new aws.S3().listBuckets({}).promise();
// Thenexpect(listBuckets).toHaveBeenCalledWith({});
—
Reply to this email directly, view it on GitHub
<#256 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAOCF4YTZV252GCFPOMDUZLUU2YJTANCNFSM5JJQSDPQ>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
I'm not familiar with this issue, but it seems related to a test failure we're getting after upgrading from 5.5.0. After upgrading to 5.5.1 , we have a Jest test that fails saying it's receiving an extra argument, so we had to add |
Thanks for the input @aardvarkk , are your tests defined in a similar way to those of @mjdickinson? |
@thomaux Our test looks like this:
Previously, we didn't need that second argument ( |
I did not had time to investigate more but the 5.5.x update are still breaking working tests: loopingz/webda.io#148 |
Version 5.5.0 broke existing mocks using jest.fn.
The "callback" parameter is nolonger forwarded to the mock function.
These changes seem to have broken it:
aed50d1#diff-e727e4bdf3657fd1d798edcd6b099d6e092f8573cba266154583a746bba0f346R119
replace()
is not called with cb.Minimal repro test with Jest:
Unless the intent is to only support direct returns from mock functions rather than callback calls.
I'd classify that as a major release though, not a minor 😅
I'm also not sure how you could easily support both approaches.
The text was updated successfully, but these errors were encountered: