#✅ - I want to run tests using MockAPI during an Amplify build.

1 messages · Page 1 of 1 (latest)

wraith roost
#

I want to run tests using MockAPI during an Amplify build.

I am running tests using the mock API with amplify mock api in my tests.
When I run amplify mock api in amplify.yml, I get the following error

AmplifyException [EnvironmentNotInitializedError]: Current environment cannot be determined.
```

Can I start Mock-API during Amplify build?
real igloo
#

Hey 👋 at what step in your amplify.yml/buildspec file are you invoking the mock command?

wraith roost
#

thank you for your helping!

this is amplify.yml.
amplify mock api and npm run ci commands work on local env.

version: 1
backend:
  phases:
    preBuild:
      commands:
        - amazon-linux-extras enable corretto8
        - yum install -y java-1.8.0-amazon-corretto java-1.8.0-amazon-corretto-devel
        - npm ci
        - npm run ci # this script calls `amplify mock api`.
    build:
      commands:
        - '# Execute Amplify CLI with the helper script'
        - amplifyPush --simple
frontend:
  phases:
    preBuild:
      commands:
        - npm ci
    build:
      commands:
        - npm run build
  artifacts:
    baseDirectory: build
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*

a part of package.json

  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "test:ci": "CI=true npm run test",
    "eject": "react-scripts eject",
    "start-server": "amplify mock api",
    "ci": "start-server-and-test start-server http://localhost:20002 test:ci"
  },
#

@real igloo

real igloo
wraith roost
#

@real igloo
When I tried to write in the test phase, I were able to confirm that the mock server was up and running! Thank you very much!

I have another question.

I am assuming that the test phase is run after the backend/frontend's build and deploy are done.
If the test fails, does that mean that the backend and frontend are already deployed with the code including error?
(I was trying to run the test before the amplifyPush because I wanted to make sure the test would error first).

real igloo
#

you could use a custom init/pull script instead of amplifyPush --simple to initialize your project in the runner, then run your tests before the backend "build" step, where the "build" step more or less only runs amplify push

wraith roost
#

@real igloo
thank you for replying!

I’ve already tried to do that by using amplify-cli headless mode.
(https://docs.amplify.aws/cli/usage/headless/)

but, headless-mode’s init command requires accessKeyId and secretAccessKey, I think this is not best-practice.
I wanna avoid to use accessKeyId and secretAccessKey for security risk, if I can.

Is headless-mode-cli only option for initialising amplify?
if you have another way, I wanna know that.

civic widget
#

Great feature request I would say! 😉

real igloo
wraith roost
wraith roost
#

I've confirmed that you can use the default profile in the Amplify build pipeline (amplify.yml). All my problems have been resolved; thank you!

I assume that the test phase runs after the backend/frontend builds and deploys are completed.
If the test fails, does that mean the backend and frontend are already deployed with the code containing errors?
(I was trying to run the test before the amplifyPush because I wanted to make sure the test would fail first).

I plan to make a feature proposal regarding running tests before building and deploying.
How can I do that?

@real igloo

civic widget
verbal relicBOT
#

✅ - I want to run tests using MockAPI during an Amplify build.