aws-amplify / aws-amplify/amplify-hosting
How can I use 'amplify mock api' in the test phase of the build without modifying the live database?
- Dominant language
- Dockerfile
- Stars
- 481
- Forks
- 123
- PR merge metrics
- No merged PRs in 30d
Description
### Amplify CLI Version
10.4.1
### Question
Using the suggestions in [this thread](https://github.com/aws-amplify/amplify-cli/issues/5314), 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)
```
Contributor guide
Research direction
Start with amplify.yml and startMockApi.sh, then review the linked issue thread for the intended test-phase setup. Trace which environment the Cypress tests and seed/clear steps use during the Amplify build. Done means the automated tests run against a local or isolated database without modifying live data.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, cypress
- Domain
- ci-cd, cloud, databases, testing-qa
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100