"MissingRequiredParameter" error in SDK's "param_validator.js" module seems to break or supersede mock
Opened this issue · 1 comments
ceberz commented
I stumbled on this when the code I was testing had provided undefined as a parameter for SQS.sendMessage().
When all params that the AWS SDK expects are present, then the mock works, and all of my mocked behavior gets executed during the test. When any required params are missing from the parameter object, the mocked behavior is never invoked, and the call throws a "MissingRequiredParameter" error.
Sample test code:
import { handler } from "./my-handler";
import AWS from "aws-sdk";
import AWSMock from "aws-sdk-mock";
describe("My handler", function () {
it("should do something", async () => {
AWSMock.setSDKInstance(AWS);
AWSMock.mock("SQS", "sendMessage", (params, callback) => { callback(null, {foo: "bar"}); });
const result = await handler(event);
expect(result).toMatchObject({
statusCode: 201,
});
AWSMock.restore("SQS");
});
});
Sample unit code:
import AWS from "aws-sdk";
AWS.config.apiVersions = { sqs: "2012-11-05" };
export const handler = async (event) => {
const SQS = new AWS.SQS();
const messageBody: string = JSON.stringify(event.body);
const params: SendMessageRequest = {
MessageBody: messageBody,
// QueueUrl: process.env.WORKSHOP_PERSIST_QUEUE_URL,
QueueUrl: undefined,
};
let result: SendMessageResult;
try {
result = await SQS.sendMessage(params).promise();
return {
statusCode: 201,
body: JSON.stringify(result),
};
} catch (e) {
return {
statusCode: 404,
body: JSON.stringify(e),
};
}
};
Snippet of package.json with dependencies:
"dependencies": {
"jest": "^27.0.1",
"mathjs": "9.3.2",
"source-map-support": "0.5.19"
},
"devDependencies": {
"@babel/core": "7.14.0",
"@babel/preset-env": "7.14.0",
"@babel/preset-typescript": "7.13.0",
"@tsconfig/node14": "1.0.0",
"@types/aws-lambda": "8.10.76",
"@types/jest": "26.0.23",
"@types/mathjs": "6.0.11",
"@types/node": "15.12.1",
"@typescript-eslint/eslint-plugin": "^4.25.0",
"@typescript-eslint/parser": "^4.25.0",
"aws-sdk": "2.923.0",
"aws-sdk-mock": "5.1.0",
"babel-jest": "26.6.3",
"eslint": "7.27.0",
"eslint-config-prettier": "8.3.0",
"husky": "6.0.0",
"prettier": "2.3.0",
"pretty-quick": "3.1.0",
"rimraf": "3.0.2",
"ts-jest": "^27.0.1",
"typescript": "4.2.4"
}
gargraman commented
If in your code you are using aws sdk's getQueueUrl() method, you might need to mock that as well.
In the above unit code, I assume the QueueURL is populated correctly
AWS.mock('SQS', 'getQueueUrl',function(params, callback){
callback(null, {
QueueURL: 'sqs://testurl',
})
});