5

Im trying to use gitlab runner to test and build my node server but I've run into a small issue when trying to automate tests. In my package.json I have scripts

"scripts": {
    "start": "node app.js",
    "test-init": "node ./node_modules/jasmine/bin/jasmine.js init",
    "test": "set NODE_ENV=Dev&& node ./node_modules/jasmine/bin/jasmine.js"
},

So NODE_ENV=Dev will load a different settings file. One that uses the mongodb url "mongodb://mongo/DBName" and when I run npm test on localhost the server crashes(as its supposed to) because it cant connect to mongo using the Dev setttings file. But when I run the project in GitLab on a runner it wont connect to the db as it uses the non-dev settings file which has a url. Is there any reason in the GitLab-ci why the NODE_ENV is not being set? Below is my GitLab-ci.yml

image: node:latest

stages:
  - build
  - test

cache:
  paths:
    - node_modules/

services:
  - mongo

install_dependencies:
  stage: build
  script:
    - npm install
  artifacts:
    paths:
      - node_modules/

test_with_lab:
  stage: test
  script: 
    - npm run test-init
    - npm test

1 Answer 1

5

This is because the docker images run on gitlab are linux based and therefore the set command won't work.

There are two solutions.

Solution 1

Use cross-env npm module as documented here by doing the following:

Install cross-env like so:

npm install --save-dev cross-env

Then edit your package.json to this:

"scripts": {
    "start": "node app.js",
    "test-init": "node ./node_modules/jasmine/bin/jasmine.js init",
    "test": "cross-env NODE_ENV=Dev node ./node_modules/jasmine/bin/jasmine.js"
},

Solution 2

Just modify the script for linux instead, much quicker and simpler. Here is how it should look. Note npm run test won't work on windows anymore. To avoid this use the first solution above.

"scripts": {
    "start": "node app.js",
    "test-init": "node ./node_modules/jasmine/bin/jasmine.js init",
    "test": "NODE_ENV=Dev node ./node_modules/jasmine/bin/jasmine.js"
},

Note: solution 1 is better in the long run while solution 2 is quick but dirty

Sign up to request clarification or add additional context in comments.

2 Comments

Took your advice and went with cross-env worked exactly how I wanted cheers
Glad I could help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.