【问题标题】:How to run jest by lerna in github actions如何通过 lerna 在 github 操作中运行 jest
【发布时间】:2021-09-13 02:36:55
【问题描述】:

我正在尝试在 github 操作中为 lerna 维护的 monorepo 项目开玩笑。

name: Run Unit Test

on:
  push:
    branches:
      - master
      - dev
  pull_request:
    branches:
      - master
      - dev
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Cache node modules
      uses: actions/cache@v2
      env:
        cache-name: cache-node-modules
      with:
        # npm cache files are stored in `~/.npm` on Linux/macOS
        path: ~/.npm
        key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
        restore-keys: |
          ${{ runner.os }}-build-${{ env.cache-name }}-
          ${{ runner.os }}-build-
          ${{ runner.os }}-

    - name: Install Dependencies
      run: npm -v && npm install

    - name: Run Unit Test
      run: npm run test
    
    - name: Coveralls
      uses: coverallsapp/github-action@master
      with:
        github-token: ${{ secrets.GITHUB_TOKEN }}

在 github 操作中运行测试时出现jest: not found 错误。 你可以看到错误日志here。但是当我在自己的 macOS 中运行 npm installnpm run test 时,一切正常。

这是我的 root 的 package.json:

{
  "private": true,
  "description": "a progressive micro frontend library",
  "workspaces": [
    "packages/*"
  ],
  "scripts": {
    "postinstall": "lerna bootstrap",
    "bootstrap": "lerna bootstrap",
    "clean": "lerna clean",
    "test": "lerna run test --stream",
    "build": "lerna run build --stream",
    "commit": "git cz",
    "lint": "lerna run lint --stream",
    "publish": "lerna publish",
    "docs": "docsify serve docs"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/ObviousJs/obvious.git"
  },
  "author": "Philip Lau",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/ObviousJs/obvious/issues"
  },
  "homepage": "https://github.com/ObviousJs/obvious#readme",
  "publishConfig": {
    "access": "public"
  },
  "config": {
    "commitizen": {
      "path": "node_modules/cz-conventional-changelog"
    }
  },
  "devDependencies": {
    "@rollup/plugin-commonjs": "20.0.0",
    "@rollup/plugin-node-resolve": "13.0.4",
    "@typescript-eslint/eslint-plugin": "4.31.0",
    "@typescript-eslint/parser": "4.31.0",
    "commitizen": "4.2.4",
    "cz-conventional-changelog": "3.3.0",
    "docsify-cli": "4.4.3",
    "eslint": "7.32.0",
    "eslint-config-standard": "16.0.3",
    "eslint-plugin-import": "2.24.2",
    "eslint-plugin-node": "11.1.0",
    "eslint-plugin-promise": "5.1.0",
    "husky": "4.3.8",
    "lerna": "4.0.0",
    "rollup": "2.56.3",
    "rollup-plugin-typescript2": "0.30.0"
  },
  "husky": {
    "hooks": {
      "pre-commit": "npm run lint && npm run build && git add ./"
    }
  }
}

这是子项目的package.json:

{
  "name": "@obvious/core",
  "version": "0.4.0",
  "description": "a  progressive micro front framework",
  "main": "./dist/index.umd.js",
  "module": "./dist/index.es.js",
  "types": "./dist/index.d.ts",
  "scripts": {
    "build": "rollup -c rollup.config.js",
    "lint": "eslint --fix --ext .ts,.js test",
    "test": "jest --coverage"
  },
  "author": "Philip Lau",
  "license": "MIT",
  "dependencies": {
    "@vue/reactivity": "3.2.10",
    "tslib": "2.3.1"
  },
  "devDependencies": {
    "@types/jest": "27.0.1",
    "jest": "27.1.1",
    "nock": "13.1.3",
    "node-fetch": "2.6.2",
    "ts-jest": "27.0.5",
    "typescript": "4.4.3"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/ObviousJs/obvious-core.git"
  },
  "bugs": {
    "url": "https://github.com/ObviousJs/obvious-core/issues"
  },
  "homepage": "https://github.com/ObviousJs/obvious-core#readme"
}

我的 github 仓库地址是https://github.com/ObviousJs/obvious-core

对不起,我的泳池英语,但我真的需要一些帮助(orz

【问题讨论】:

  • 没有节点你要如何运行 jest?

标签: jestjs github-actions lerna


【解决方案1】:

我对此知之甚少,但对于一个临时答案,对我有用的东西(当我遇到同样的笑话时)正在添加,

- run: lerna bootstrap --no-ci

在我的工作流配置中运行我的npm test 命令之前。因此,我最终得到了这样的工作流程:

on:
  pull_request:
    branches: [ master ]

jobs:
  test_pull_request:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: 12
      
      # fix issue with lerna and clean installs
      - run: lerna bootstrap --no-ci  
      - run: npm test
【解决方案2】:

我终于通过设置解决了这个问题 useWorkspaces: false 在 lerna.json 中

原因是 npm6.x 不支持工作空间。而github-action runner的npm版本只是v6,所以package.json中的workspaces是没有用的,所以我们应该把lerna.json中的useWorkspaces设置为false

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-27
    • 1970-01-01
    • 2021-05-02
    • 2022-01-26
    • 2022-01-12
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    相关资源
    最近更新 更多