dwyl / aws-sdk-mock

:rainbow: AWSomocks for Javascript/Node.js aws-sdk tested, documented & maintained. Contributions welcome!
Apache License 2.0
1.11k stars 109 forks source link

aws mock s3 putObjectTagging "Not Working" ISSUE / Bug #266

Open hanoj-budime opened 2 years ago

hanoj-budime commented 2 years ago

! important

S3 putObjectTagging mock not working, why any ideas..... ???


look.test.js

const AWS = require("aws-sdk-mock");

describe('s3 putObjectTagging', () => {
...
    beforeAll(async () => {
         // Arrange
        AWS.mock("S3", "putObjectTagging", jest.fn((params, callback) => {  // Not working
          callback(null, {});
        }));
      ....
    });
    ....
    // Assert
    ....
});

package.json

"dependencies": {
    "aws-sdk-mock": "^5.6.2", // aws-sdk mock
    "jest": "^27.5.1",
    "jest-junit": "^13.0.0",
    "jest-runner-eslint": "^1.0.0"
  }
nelsonic commented 2 years ago

@HanojHanu what are you seeing when you attempt to run this code? (what is the output?)

hanoj-budime commented 2 years ago

@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)
hanoj-budime commented 2 years ago

@nelsonic Any updates on this issue ?

hanoj-budime commented 2 years ago

@nelsonic @thomaux

nelsonic commented 2 years ago

@HanojHanu the stack trace you've shared above is for aws-sdk ... You're getting an error when using the actual SDK.

thomaux commented 2 years ago

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();
});
hanoj-budime commented 2 years ago

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..

hanoj-budime commented 2 years ago

tried the same way, what you suggested. @thomaux But still the same error. https://github.com/dwyl/aws-sdk-mock/issues/266#issuecomment-1076426980

hanoj-budime commented 2 years ago

https://stackblitz.com/edit/node-as6ark?file=index.test.js

@thomaux , @nelsonic

thomaux commented 2 years ago

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.

hanoj-budime commented 2 years ago

Any Fix?