Amplify Mock Commands

Pre-requisite

Java is required on your development workstation to use Local Mocking in Amplify

Which package is used?#

If you go into the $HOME_DIR/.amplify/plugins.json file (where $HOME_DIR is your home directory), which lists all the plugins available on your system, you might notice the following lines at the end:

"mock": [
{
"packageName": "amplify-util-mock",
"packageVersion": "3.27.7",
"packageLocation": "$HOME_DIR/.config/yarn/global/node_modules/amplify-util-mock",
"manifest": {
"name": "mock",
"type": "util",
"commands": [
"api",
"function",
"mock",
"storage",
"help"
],
"eventHandlers": []
}
}
]

This indicates that the amplify-util-mock package is used when we invoke mocking commands.

What commands are available?#

As per the above JSON property, we can infer that the following commands are available for invocation:

# To mock the AWS AppSync API
amplify mock api
# To mock AWS Lambda Functions
amplify mock function
# To mock all available local resources
amplify mock
# To mock storage solutions (Amazon S3 or Amazon DynamoDB)
amplify mock storage
# Help!
amplify mock help

Folder Structure#

# /amplify-util-mock/src
├── amplify-plugin-index.ts # Handles the entry point
├── api # AppSync Mocking
├── CFNParser # CloudFormation Template Parser
├── commands # Handles the mock commands
├── __e2e__ # E2E Tests
├── func # For mocking Lambda functions
├── index.ts # Entry point of all commands
├── mockAll.ts # Mocking all the available resources
├── storage # Mocking Storage
├── __tests__ # Tests
└── utils # Utility Programs

Details of files and folders#

export async function executeAmplifyCommand(context: any) {
const commandPath = path.normalize(
path.join(__dirname, "commands", pluginName, context.input.command)
);
const commandModule = await import(commandPath);
await commandModule.run(context);
}

The executeAmplifyCommand determines the sub-command and calls the run() function in the appropriate file located in the commands/mock directory.