jestjs - Github Actions - 运行服务器和前端,然后执行测试

标签 jestjs continuous-integration cypress github-actions

我想使用 Github Actions for CI 并在分支合并之前运行测试。

我有一个存储库,其中包含我的服务器和前端(Nest 和 Angular)。
我正在使用 Cypress/Jest 进行测试。

我需要运行后端服务器才能通过前端 cypress 测试。
目前 GH Actions 不会进入下一步,因为后端进程正在运行 - 但这是我需要发生的......

我应该如何设置才能使用 GH Actions for CI?

name: test
on: [push]
env:
  CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  OTHER_SECRETS: ${{ secrets.otherSecrets }}
jobs:
  cypress-run:
    runs-on: macos-11
    steps:
      # start cypress w/github action: https://github.com/cypress-io/github-action
      - name: Setup Node.js environment
        uses: actions/setup-node@v2.5.0
        with:
          node-version: '16.13.0'
      - name: Checkout
        uses: 'actions/checkout@v2'
      - name: "Start Backend"
        run: |
          cd server &&
          npm install &&
          npm run build &&
          npm run start:prod
      - name: "Start Frontend"
        run: |
          npm install &&
          npm run build &&
          npm run start
      - name: Cypress run
        uses: cypress-io/github-action@v2
        with:
          record: true
          browser: chrome
      - name: "Run Jest Tests"
        run: |
            cd server &&
            npm run test

#note:我尝试将“&& sleep 10 && curl http://localhost:port -i”选项附加到 npm 命令 - 但它对我不起作用。

#note2:这是我第一次使用 GH Actions,所以我可能遗漏了一些明显的东西!!

最佳答案

#note: I have tried appending the "&& sleep 10 && curl http://localhost:port -i" option to the npm commands - and it hasn't worked for me.

这里有一个小错误,&&会等待上一条命令完成,只有成功才运行下一条 &会在中运行上一条命令背景,然后将继续运行下一个。因此,由于没有什么能阻止您的服务器,&& 将不起作用。

我不确定这是最干净的方法,但以下方法应该有效,我在我的一个项目中使用了等效的方法来运行 UI。

  - name: "Start Backend"
    run: |
      cd server &&
      npm install &&
      npm run build &&
      npm run start:prod &
      sleep 5 &&
      curl http://localhost:port -I
  - name: "Start Frontend"
    run: |
      npm install &&
      npm run build &&
      npm run start &
      sleep 5 &&
      curl http://localhost:port -I

关于jestjs - Github Actions - 运行服务器和前端,然后执行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70231685/

相关文章:

GitHub Google Cloud Build - 多个存储库

node.js - 如何使用 Jest 模拟测试 Node.js CLI?

javascript - 开 Jest 抛出 TyperError : Expected a function when using lodash pipe/flow

reactjs - 使用react-testing-library在表单中提交时如何测试已调用的函数?

Angular - 添加 Cypress data-cy 属性

javascript - 节点JS : NOT able to set PERCY_TOKEN via package script with start-server-and-test

javascript - Cypress select 没有值(value)

javascript - 如何正确地让 mock 在 Jest 中抛出错误?

continuous-integration - 如何防止跨多个Build Agent的每个构建配置并行构建

visual-studio - "delayed commit"如何与源代码控制存储库和 CI 服务器一起工作?