question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mock function not using mock API

See original GitHub issue

Note: If your issue/bug is regarding the AWS Amplify Console service, please log it in the Amplify Console GitHub Issue Tracker

Describe the bug When testing my lambda function via mock, the API_MYPROJECT_GRAPHQLAPIENDPOINTOUTPUT environment variable points to amazonaws.com even though the mock api server is running.

Amplify CLI Version 4.41.1

To Reproduce Terminal 1: amplify mock Terminal 2: amplify status Terminal 3: amplify mock function mylambda --event src/event.json --timeout 7

Expected behavior amplify status should show the AppSync mock endpoint that’s running. The API_MYPROJECT_GRAPHQLAPIENDPOINTOUTPUT environment variable should point to the mock endpoint.

Desktop (please complete the following information):

  • OS: Mac BigSur 11.1 Macbook Pro M1
  • Node Version v14.12.0

Additional context The react app DOES correctly switch between the cloud and the mock endpoint. GraphiQL explorer runs on localhost.

Thanks

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dmost714commented, Mar 2, 2021

@m2broth Yes, my workaround it to overwrite the APIKEYOUTPUT variable when mock is running.

In index.js I add the following:

exports.handler = async (event) => {
    //console.log('process.env', process.env)
    //console.log('event', JSON.stringify(event))
    if (process.env.AWS_EXECUTION_ENV === 'AWS_Lambda_amplify-mock') {
        console.log("BUGFIX: overwrite GRAPHQLAPIKEYOUTPUT to use mock key")
        process.env.API_AMPYMAIL_GRAPHQLAPIKEYOUTPUT = 'da2-fakeApiId123456'
    }

Hope that helps.

(PS: If you’re also using a mock GraphQL, beware it behaves differently than production for queries that return non-nullable results: https://github.com/aws-amplify/amplify-cli/issues/6450)

1reaction
dmost714commented, Jan 14, 2021

@attilah Exactly correct.

I’d like to do all my testing locally. The webapp (react) is using the mocked graphql and S3. I have an amplify generated lambda function (a webhook that will be called by a 3rd party). When I mock test the function, it always writes data to the cloud instead of the local gql server.

I have 3 terminals, one in Visual Studio running the webapp, one running “amplify mock” for api & storage, and one to mock trigger the lambda function.

Thank you, I’m enjoying using Amplify very much.

-d-

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mock Functions - Jest
You can create a mock function with jest.fn() . If no implementation is given, the mock function will return undefined when invoked.
Read more >
The only 3 steps you need to mock an API call in Jest
Use this newfound power to give your functions exactly what they should expect from the API calls. Stop worrying about what the network...
Read more >
Mock function not called and not resolving or returning ...
I am using jest to test the axios API calls. Mocking the API is not resolving or returning a value and mock function...
Read more >
The only 3 steps you need to mock an API call in Jest
When you run jest.mock('axios') , Jest replaces every function in the axios module with empty "mock" functions. These mock functions essentially ...
Read more >
Mock Functions · Jest
Accepts a function that will be used as an implementation of the mock for one call to the mocked function. Can be chained...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found