我尝试在GitLab Ci/Cd管道中运行一个非常简单的摩卡测试。
我的应用程序使用nodejs、express和typescript。
目录结构如下所示:
- root
- backend
- src
- index.ts
- tests
- chai
- index.ts
- Dockerfile
- nodedemon
- package.json
- package_lock.json
- tsconfig.json
- .gitlab-ci.yml
- docker-compose.yml
使用src/index.ts创建快速服务器。
chai/index.ts看起来像这样:
import chaiHttp from "chai-http";
import chai from "chai";
import {app} from "../../index";
chai.use(chaiHttp);
// Test base route to return string
describe("Base Route Test", () => {
it(`should return a html file`, () => {
return chai.request(app).get("/")
.then(res => {
chai.expect(res).to.be.html
})
})
})
nodemon文件如下所示:
{
"watch": ["src"],
"ext": ".ts",
"ignore": [],
"exec": "ts-node ./src/index.ts"
}
停靠文件:
FROM node:16.13.0
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8008
RUN npm run build
ENTRYPOINT ["npm", "run", "start"]
docker-compose.yml:
version: '3.0'
services:
mongo:
image: mongo
container_name: cargonaut_mongo
ports:
- "27017:27017"
node:
build: ./backend
container_name: cargonaut
ports:
- "8008:8008"
depends_on:
- mongo
environment:
- "NODE_ENV=${NODE_ENV}"
和package.json(gitlab示例和名称使用--进行审查):
{
"name": "cargonaut",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon",
"build": "npx tsc",
"test": "mocha -r ts-node/register src/tests/chai/index.ts"
},
"repository": {
"type": "git",
"url": "git@-------:-----/cargonaut.git"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/config": "^0.0.41",
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/mocha": "^9.1.1",
"@typescript-eslint/eslint-plugin": "^5.27.0",
"@typescript-eslint/parser": "^5.27.0",
"eslint": "^8.18.0",
"nodemon": "^2.0.16",
"ts-node": "^10.8.0",
"typescript": "^4.7.2"
},
"dependencies": {
"@babel/types": "^7.18.4",
"@types/chai": "^4.3.1",
"@types/express-session": "^1.17.4",
"@types/node": "^18.0.0",
"body-parser": "^1.20.0",
"chai": "^4.3.6",
"chai-http": "^4.3.0",
"config": "^3.3.7",
"cors": "^2.8.5",
"express": "^4.18.1",
"express-session": "^1.17.3",
"json5": "^2.2.1",
"mocha": "^10.0.0",
"mongoose": "^6.4.0"
}
}
最后是gitlab-ci.yml:
docker build:
stage: build
tags:
- npm
before_script:
- cd backend
- npm install
script:
- NODE_ENV=test_docker_free npm run test
这个命令在我的计算机上可以工作,但是如果我推入到我的repo,管道将失败,并显示以下输出:
$ NODE_ENV=test_docker_free npm run test
npm info it worked if it ends with ok
npm info using npm@6.9.0
npm info using node@v10.16.1
npm info lifecycle cargonaut@1.0.0~pretest: cargonaut@1.0.0
npm info lifecycle cargonaut@1.0.0~test: cargonaut@1.0.0
> cargonaut@1.0.0 test /builds/----/cargonaut/backend
> mocha -r ts-node/register src/tests/chai/index.ts
✖ ERROR: Error: Not supported
at formattedImport (/builds/----/cargonaut/backend/node_modules/mocha/lib/nodejs/esm-utils.js:30:3)
at exports.requireOrImport (/builds/----/cargonaut/backend/node_modules/mocha/lib/nodejs/esm-utils.js:38:34)
at exports.handleRequires (/builds/----/cargonaut/backend/node_modules/mocha/lib/cli/run-helpers.js:94:34)
at process._tickCallback (internal/process/next_tick.js:68:7)
at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
npm info lifecycle cargonaut@1.0.0~test: Failed to exec test script
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! cargonaut@1.0.0 test: mocha -r ts-node/register src/tests/chai/index.ts
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the cargonaut@1.0.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm timing npm Completed in 469ms
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2022-06-26T09_56_58_783Z-debug.log
Cleaning up project directory and file based variables 00:01
ERROR: Job failed: exit code 1
我发现了这个错误报告:https://github.com/mochajs/mocha/issues/4652,但我不确定它是否与我的情况相关,因为我使用的是mocha 10. 00和node v16. 13. 0,这个问题似乎会影响旧版本。
那么错误的原因是什么呢?有没有办法修复它?
3条答案
按热度按时间ubby3x7f1#
将Node JS升级到最新版本。以下方法对我有效:
9bfwbjaz2#
我看来问题不在代码上:我的大学GitLab的运行程序非常过时。
因此,如果有人遇到同样的问题,检查您的GitLab runner使用的节点版本可能值得一试。
可悲的是,要解决这个问题,我没有太多的事情可以做,我只能切换到GitHub。
kwvwclae3#
只需从v16.17.0安装NodeJS,没有更多错误;))