Randy Song Randy Song - 1 year ago 80
Node.js Question

Mocha test that runs locally won't run on Jenkins (environment issue)

I'm currently trying to implement CI with Jenkins for an emberjs node project that uses mocha for unit testing. I'm running Jenkins on an Amazon EC2 server.

When I run mocha locally (both on my desktop, AND on the ec2 server) I get this:

Initializing server on port 8090

Unit Test for /test
test API call incoming
key res value is: test!
✓ gives a json object with res: test!

1 passing (35ms)

However, when I have jenkins set up to run this same command:

01:44:46 + ./node_modules/mocha/bin/mocha
01:44:46 /var/lib/jenkins/workspace/Rekindle2_Node/server/routes/test/getTest.js:4
01:44:46 const getTest = (req, res) => {
01:44:46 ^
01:44:46 SyntaxError: Unexpected token >

I've double checked, the package.json has everything I need, and I know that I don't have anything globally installed that changes things (as I managed to git clone, npm install, and run mocha from the ec2 instance). All I know is that jenkins sometimes can have issues with consuming environment variables from the server it's running on? Does anyone know what the issue I'm encountering could be? I've tried uninstalling and reinstalling node as well, it could be that Jenkins is looking at an older installation of node while the ec2 is not. Is there any way to tell this? How do I look at the environment variables of a specific build?

Answer Source

That looks like Jenkins is using an older version of node.js. To confirm this I would add logging to your Jenkins job. node --version will print the version of node.js that Jenkins is aware of. Also if you are interested in the environment there is a view in the Jenkins web ui to show the environment from a particular build. Or you can add env as a line in your Jenkins build script to print out the current environment variables. If it turns out to be a node.js version issue, I would look into using a plugin to manage your nodejs versions: node.js Jenkins plugin

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download