如何 运行 lerna 在 github 动作中开玩笑

How to run jest by lerna in github actions

我正在尝试 运行 开玩笑一个由 lerna 在 github 行动中维护的 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。但是当我在自己的 macO 中 运行 npm installnpm run test 时,一切正常。

这是我的 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

我对此一无所知,但对于一个临时答案,对我有用的(当我在开玩笑时遇到同样的错误时)是添加,

- 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

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

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