Amplify-cli: Mock Node ExpressJs function always returns null

Created on 18 Apr 2020  路  6Comments  路  Source: aws-amplify/amplify-cli

event object = {
"httpMethod": "GET",
"body": "{\"name\": \"Amplify\"}",
"path": "/test",
"resource": "/{proxy+}",
"queryStringParameters": {}
}

app.js get method =

/******

  • Example get method *
    ******/

app.get("/test", function (req, res) {
// Add your code here
console.log("test");
res.json({ success: "get call succeed!", url: req.url });
});

Which Category is your question related to?
Amplify invoke function (mock functino)

Amplify CLI Version

4.18.0

What AWS Services are you utilizing?

Amplify mock function to locally host a nodejs based lambda function for testing.

Provide additional details e.g. code snippets

Mocking the function always returns null, no matter what event is used whether its valid or not.

PS C:\Users\User\Desktop\Startup\amplify_sandbox> amplify mock function testFunction

? Provide the path to the event JSON object relative to
C:\Users\User\Desktop\Startup\amplify_sandbox\amplify\backend\function\testFunction src/event.json

Starting execution...

EVENT: "{\"httpMethod\":\"GET\",\"body\":\"{\\"name\\": \\"Amplify\\"}\",\"path\":\"/test\",\"resource\":\"/{proxy+}\",\"queryStringParameters\":{}}"

Result:
null

Finished execution.

bug functions

Most helpful comment

This should be fixed in the latest release of the CLI (4.20.0) thanks to @bboure!

All 6 comments

Any help would appreciated!

I've found a workaround to locally test my express js function. using nodemon to invoke the app.js and using dotenv to locally store my env variables, but this is very tedious. Maybe a feature that uses nodemon to host a local server (supports hot reload) and pass env variables in the process would be beneficial!

This broke recently - the awsServerlessExpress doesn't even appear to boot up before returning a response.

Looks like the serverless template that comes out of the box uses the deprecated context.succeed() lambda operation. Until we can get a release out with an update to the template, you'll need to update the last line of the handler in index.js to

return awsServerlessExpress.proxy(server, event, context, 'PROMISE').promise;

That will allow mock to work with the handler.

As for the additional environment variable support, we are actively working on a more comprehensive solution to that problem

You may also want to follow #4019 regarding issues with mock not working with the callback function. @bboure is working on a fix there

This should be fixed in the latest release of the CLI (4.20.0) thanks to @bboure!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MageMasher picture MageMasher  路  3Comments

amlcodes picture amlcodes  路  3Comments

rehos picture rehos  路  3Comments

adriatikgashi picture adriatikgashi  路  3Comments

onlybakam picture onlybakam  路  3Comments