aws-amplify / amplify-hosting

AWS Amplify Hosting provides a Git-based workflow for deploying and hosting fullstack serverless web applications.
https://aws.amazon.com/amplify/hosting/
Apache License 2.0
456 stars 115 forks source link

How can I use 'amplify mock api' in the test phase of the build without modifying the live database? #3280

Open cjsilva-umich opened 1 year ago

cjsilva-umich commented 1 year ago

Amplify CLI Version

10.4.1

Question

Using the suggestions in this thread, I have configured my amplify.yml file to run 'amplify mock api' and run my cypress tests during the build phase of my amplify project. Part of the cypress tests involves clearing out the database and providing initial seed data.

This works exactly like I expect it to locally on my machine. 'amplify mock api' creates a local database and, if there's any existing data in it from a previous run, it clears it all out before creating the initial data needed to run the app. Then, when I'm done with my tests, I ctrl-c my process to stop mocking the api, my app targets the remote data, and it's all as it was before my tests.

However, now that I've got the tests running as part of the build phase, the clearing/seeding process is happening to my live data. Thankfully I'm just doing this all on a personal development branch, but if it had happened on a production environment it could have been disastrous.

What is happening in the build process that is not targeting the locally created/mocked database? How can I get this to behave like I want where the automated cypress runs in the test phase are not using my live data?

Here is my amplify.yml file contents (nearly identical to those from the linked issue):

version: 1
test:
  phases:
    preTest:
      commands:
        - yum install -y java-11-amazon-corretto-headless
        - rm -rf node_modules
        - yarn cache clean
        - yarn install --immutable
        - yarn add ts-node
        - yarn add wait-on
        - yarn add pm2
        - yarn add mocha
        - yarn add mochawesome
        - yarn add mochawesome-report-generator
        - yarn add mochawesome-merge
        - yarn pm2 start startMockApi.sh
        - yarn wait-on http://localhost:20002
        - yarn pm2 start 'yarn start'
        - yarn wait-on http://localhost:3000
    test:
      commands:
        - echo "REACT_APP_CYPRESS_ADMIN_USERNAME=$REACT_APP_CYPRESS_ADMIN_USERNAME" >> .env
        - echo "REACT_APP_CYPRESS_ADMIN_PASSWORD=$REACT_APP_CYPRESS_ADMIN_PASSWORD" >> .env
        - echo "REACT_APP_CYPRESS_USER_USERNAME=$REACT_APP_CYPRESS_USER_USERNAME" >> .env
        - echo "REACT_APP_CYPRESS_USER_PASSWORD=$REACT_APP_CYPRESS_USER_PASSWORD" >> .env
        - yarn run cypress run --reporter mochawesome --reporter-options 'reportDir=cypress/report/mochawesome-report,overwrite=false,html=false,json=true,timestamp=mmddyyyy_HHMMss'
    postTest:
      commands:
        - yarn mochawesome-merge cypress/report/mochawesome-report/*.json > cypress/report/mochawesome.json
  artifacts:
    baseDirectory: cypress
    configFilePath: "**/mochawesome.json"
    files:
      - "**/*.png"
      - "**/*.mp4"
frontend:
  phases:
    preBuild:
      commands:
        - yarn install
    build:
      commands:
        - REACT_APP_ENV=${REACT_APP_ENV}
        - yarn run build
  artifacts:
    baseDirectory: build
    files:
      - "**/*"
  cache:
    paths:
      - node_modules/**/*

and startMockApi.sh:

(amplify mock api)
josefaidt commented 1 year ago

Hey @cjsilva-umich :wave: thanks for raising this! I'm going to transfer this over to our Hosting repo for better assistance 🙂

cjsilva-umich commented 1 year ago

Any chance that someone might be able to comment on this?

ghost commented 1 year ago

Hi @cjsilva-umich 👋🏽 apologies for the delay here. It's difficult to determine next steps without knowing how you configured your script to access the appropriate database. Is there any example code you could share to help us identify why your live data is being accessed?

cjsilva-umich commented 1 year ago

Hi @hloriana,

I don't have any additional files that configure anything to do with the mocking or the db targeting (not intentionally anyway). Do you have any suggestions about where I might look to find anything targeting the like? The only things I've put in place to try to get this working is the amplify.yml file and the startMockApi.sh file, both shown in their entirety above.

From what I can tell, Amazon doesn't have any official documentation for how to set up db mocking for the Test phase in Amplify's build and deploy process, that's why I followed the suggestions of others in the thread I linked to in my original post.

Are you able to use amplify mock api in conjunction with Cypress testing wherein a mocked database is used during the cypress tests and then the live database is targeted once more (unmodified from tests) when tests are finished and the project is deployed live again? If you are, could you please share your configuration of your amplify.yml file and, even some examples that show the live database isn't modified and that the mocked one is used during tests? It would help IMMENSELY.

Thank you!

cjsilva-umich commented 1 year ago

Anyone having any success using amplify mock api in the Test phase of the Amplify build process?

cjsilva-umich commented 1 year ago

Can anyone get back to me about this?

nestorochoa commented 1 year ago

I have a similar issue, I have a monorepo with Nx and three projects with NextJS, I need the three of them running to have a proper e2e test, but just one monorepo can get the mock awsconfig.js and the other projects keeps the awsconfig config that aims to the real db.

cjsilva-umich commented 1 year ago

Can anyone from amazon/aws please confirm for me whether or not they are able to use 'amplify mock api' in the test phase of the Amplify build and have it not modify the live database. And, if so, how they are able to do so? Been waiting a long time for help with this.