Open hanoj-budime opened 2 years ago
@HanojHanu what are you seeing when you attempt to run this code? (what is the output?)
@HanojHanu what are you seeing when you attempt to run this code? (what is the output?)
Not Mocking my code in Jest using Nodejs
Failed to tag the requested S3 Object
Unhandled error. (NoSuchBucket: The specified bucket does not exist
- -
Terminal
Test suite failed to run
Error [ERR_UNHANDLED_ERROR]: Unhandled error. (NoSuchBucket: The specified bucket does not exist
at Request.extractError (node_modules/aws-sdk/lib/services/s3.js:710:35)
at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:106:20)
at Request.emit (node_modules/aws-sdk/lib/sequential_executor.js:78:10)
at Request.emit (node_modules/aws-sdk/lib/request.js:686:14)
at Request.transition (node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (node_modules/aws-sdk/lib/state_machine.js:14:12)
at node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:688:12)
at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:116:18) {
code: 'NoSuchBucket',
region: null,
time: 2022-03-24T04:04:56.504Z,
requestId: 'AASKZ3Q97T6P1XCV',
extendedRequestId: 'wId6kZvnmgMk9LIL+Ll2iVGjqKHiaOsLzgDPdzyXnwbN1cY1po1+wGLy7BLWYcDOrcquauHxN/s=',
cfId: undefined,
statusCode: 404,
retryable: false,
retryDelay: 92.723206311658
})
at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:590:14)
at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:106:20)
at Request.emit (node_modules/aws-sdk/lib/sequential_executor.js:78:10)
at Request.emit (node_modules/aws-sdk/lib/request.js:686:14)
at Request.transition (node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (node_modules/aws-sdk/lib/state_machine.js:14:12)
at node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:38:9)
@nelsonic Any updates on this issue ?
@nelsonic @thomaux
@HanojHanu the stack trace you've shared above is for aws-sdk
... You're getting an error when using the actual SDK.
Hey @HanojHanu, this might be related to how your actual code is initialised. For example, if S3 is initialised at the top of your module, aws-sdk-mock cannot mock it. Try adding the following to your tests:
beforeEach(() => {
// import the aws-sdk after the resetModules, but before each test.
jest.resetModules();
AWSMock.setSDKInstance(require('aws-sdk'));
AWSMock.mock("S3", "putObjectTagging", jest.fn((params, callback) => { // Not working
callback(null, {});
}));
});
afterEach(() => {
AWSMock.restore();
});
Hey @HanojHanu, this might be related to how your actual code is initialised. For example, if S3 is initialised at the top of your module, aws-sdk-mock cannot mock it. Try adding the following to your tests:
beforeEach(() => { // import the aws-sdk after the resetModules, but before each test. jest.resetModules(); AWSMock.setSDKInstance(require('aws-sdk')); AWSMock.mock("S3", "putObjectTagging", jest.fn((params, callback) => { // Not working callback(null, {}); })); }); afterEach(() => { AWSMock.restore(); });
Thanks @thomaux, I'll try this.
I'll update here..
tried the same way, what you suggested. @thomaux But still the same error. https://github.com/dwyl/aws-sdk-mock/issues/266#issuecomment-1076426980
https://stackblitz.com/edit/node-as6ark?file=index.test.js
@thomaux , @nelsonic
Thanks @HanojHanu that's useful. I'll take some time later this week to take a look at what's going wrong. From the sources I can tell that S3 is being initialised at the top of the file, which is most likely causing the issue.
Any Fix?
! important
S3
putObjectTagging
mock not working, why any ideas..... ???look.test.js
package.json