首页 > 解决方案 > gitlab-ci 在monorepo中开玩笑,相互依赖

问题描述

我用 jest 在我的 monorepo 中运行测试。这些测试与 monorepo 的其他子包相互依赖。

在当地一切都很好。但是 gitlab-ci 管道失败,导致它无法解决相互依赖关系......

简化的项目结构:

packages
-core
--src
---core.js
--package.json
-advanced
--src
---advanced.js
---advanced.test.js
--package.json
.gitlab-ci.yml
jest.config.js
package.json

简单的 jest.config.js:

module.exports = {
  projects: ['packages/*'],
  rootDir: __dirname,
  roots: ['<rootDir>/packages'],
  testMatch: ['**/*.test.js'],
}

简单的核心/package.json:

{
  "name": "@myProject/core",
  "version": "1.0.0"
}

简单的高级/package.json:

{
  "name": "@myProject/advanced",
  "version": "1.0.0",
  "dependencies": {
    "@myProject/core": "^1.0.0"
  }
}

简单的advanced.test.js:

import thisthat from 'randomBibX'
import others from 'randomBibY'
import core from '@myproject/core'

//doTests

简单的 package.json:

{
  "scripts": {
    "test": "jest"
  }
  "devDependencies": {
    "randomBibX": "^1.0.0",
    "randomBibY": "^1.0.0"
  }
}

简化的 .gitlab-ci.yml:

image: node:10
stages:
  - setup
  - test
setup:
  stage: setup
  script:
    - yarn config set cache-folder /cache/.yarn
    - yarn install --non-interactive --frozen-lockfile
  artifacts:
    expire_in: 1hour
    paths:
      - node_modules
      - "packages/*/node_modules"
jest:
  stage: test
  dependencies:
    - setup
  script:
    - "[ ! -d node_modules/ ] && yarn install --non-interactive --frozen-lockfile"
    - yarn test

错误:

FAIL packages/advanced/src/advanced.test.js
   ● Test suite failed to run
     Cannot find module '@myProject/core' from 'advanced.test.js'

标签: jestjsgitlab-cimonorepo

解决方案


解决方案是,在测试之前构建项目

改进的 .gitlab-ci.yml:

image: node:10
stages:
  - setup
  - test
installAndBuild:
  stage: setup
  script:
    - yarn config set cache-folder /cache/.yarn
    - yarn install --non-interactive --frozen-lockfile
    - yarn lerna run build --stream
  artifacts:
    expire_in: 1hour
    paths:
      - node_modules
      - "packages/*/node_modules"
jest:
  stage: test
  dependencies:
    - installAndBuild
  script:
    - "[ ! -d node_modules/ ] && yarn install --non-interactive --frozen-lockfile"
    - yarn test

推荐阅读