jestjs - 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'
解决方案
解决方案是,在测试之前构建项目
改进的 .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
推荐阅读
- java - 将两个对象列表合并到 Map 中,其值为 java 8 中的不同对象
- javascript - 在异步函数中调用异步函数时事务完成错误
- swift - 快速数据类型 - Optional 的强制
- php - php将单个产品传递到带有隐藏字段的购物车
- azure-service-fabric - 处理多种参与者类型的 Service Fabric 参与者模型
- xml - xpath 请求选择具有 commun 属性的元素
- php - 尝试使用 GetResponse api 保存联系人,但我无法
- mysql - 如何在 Excel CSV 中保留日期和引号的 SQL 格式?
- r - 将 3 列连接为线性回归模型
- python - 使用火花流时找不到 kafka 的组信息