I am trying to run a very simple mocha test in a GitLab Ci/Cd pipeline.
My application uses nodejs, express and typescript.
The directory structure looks like this:
- root
- backend
- src
- index.ts
- tests
- chai
- index.ts
- Dockerfile
- nodedemon
- package.json
- package_lock.json
- tsconfig.json
- .gitlab-ci.yml
- docker-compose.yml
With src/index.ts creating the express server.
chai/index.ts looks like this:
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
})
})
})
The nodemon file looks like this:
{
"watch": ["src"],
"ext": ".ts",
"ignore": [],
"exec": "ts-node ./src/index.ts"
}
Dockerfile:
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}"
and package.json (gitlab-instance and name censored using --):
{
"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"
}
}
And finally, the gitlab-ci.yml:
docker build:
stage: build
tags:
- npm
before_script:
- cd backend
- npm install
script:
- NODE_ENV=test_docker_free npm run test
This commands work on my computer, but if I push to my repo, the pipeline fails with the following output:
$ 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
I found this bug report: https://github.com/mochajs/mocha/issues/4652 but I am not sure if it is relevant in my case, since I use mocha 10.00 and node v16.13.0 and the issue seems to affect older versions.
So what is the reason for the error? Is there a way to fix it?