emersonthis emersonthis - 1 month ago 10
Node.js Question

How to automate karma unit tests with continuous integration

Currently we run our karma/jasmine unit tests a gulp task:

gulp test


We are trying to figure out how to get circleci to run our tests automatically. I tried adding
gulp test
under the
test:
section of the circle.yml file but I get
gulp: command not found
. But I get the same error if I try something basic like
pwd
. So clearly I'm doing something wrong.

I think the same results could be achieved by using the
scripts
property in the package.json, because circleci runs that automatically, but I'm not sure how to do that.

Here's our circle.yml file...

dependencies:
override:
- echo PHP rules

test:
override:
- gulp test #this doesnt work!

deployment:
development:
branch: dev
heroku:
appname: ourapp


Here's package.json...

{
"name": "ourapp",
"private": true,
"description": "An app",
"main": "index.js",
"dependencies": {
"gulp": "~3.8.5"
},
"devDependencies": {
"karma": "~0.12.31",
"karma-chrome-launcher": "~0.1.7",
"jasmine-core": "~2.2.0",
"karma-jasmine": "~0.3.5",
"karma-firefox-launcher": "~0.1.4",
"karma-ie-launcher": "~0.1.5",
"karma-opera-launcher": "~0.1.0",
"karma-phantomjs-launcher": "~0.1.4",
"gulp": "~3.8.10",
"gulp-karma": "0.0.4",
"karma-coverage": "~0.2.7"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "The A Team"
}


Just in case it's useful, here's the gulp task...

gulp.task('test', function(){
return gulp.src(testFiles)
.pipe(karma({
configFile: 'karma.conf.js',
action: 'run' //change to watch if you want to auto-run
}))
.on('error', function(err){
throw err;
})
});


For consistency, I think the best would be if we could get circleci to run the gulp task, but it's okay if we have to run
karma ...
manually.

Answer

Here's what worked for me:

dependencies:
  pre:
    - npm install -g gulp

test:
  override:
    - gulp test

machine:
  php:
    version: 5.6.2

The npm install -g gulp was the missing piece for me. Circleci will detect package.json and run npm install automatically, but I needed the pre: to install it globally in order for my gulp task to work.

It's also worth mentioning that circleci automatically knows whether the karma/jasmine tests passed or failed by detecting the exit code. So this just works as-is.