gitlab-ci开玩笑与相互依赖的monorepo

时间:2020-04-23 09:29:01

标签: jestjs gitlab-ci monorepo

我正在开玩笑地在我的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'],
}

简化的core / package.json:

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

简化版Advanced / 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'

1 个答案:

答案 0 :(得分:0)

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

改进的.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