Setting up a Node Express Project with Mocha and Chai

As I’ve just gone through the process myself, I thought it’d be a good resource to share how I setup my Node Express project, using Mocha and Chai.

I used the express application generator as it quickly built up an application skeleton, which meant I could get started pretty quickly.

$ npm install express-generator -g 

The ‘-g’ flag ensures the package is installed globally. If you get an error (don’t worry), it’s most definitly to do with permissions on your computer and there’s an easy fix, just add sudo to the command and hit enter.

$ sudo npm install express-generator -g

This will prompt you for your user password. Once entered, the package should install successfully.

$ express -h 
Usage: express [options] [dir]
Options:
-h, --help output usage information
--version output the version number
-e, --ejs add ejs engine support
--pug add pug engine support
--hbs add handlebars engine support
-H, --hogan add hogan.js engine support
-v, --view <engine> add view <engine> support (ejs|hbs|hjs|jade|pug|twig|vash) (defaults to jade)
-c, --css <engine> add stylesheet <engine> support (less|stylus|compass|sass) (defaults to plain css)
--git add .gitignore
-f, --force force on non-empty directory

This shows the different options that can be utilised when using the express command. In my project, I used pug as a template engine, as the output suggests above, express will default to using jade, so you’d have to specify that you’d like to use pug using the -v flag. Obviously you can use another engine thats listed above, if you prefer.

$ express -v pug myappcreate : myapp
create : myapp/package.json
create : myapp/app.js
create : myapp/public
create : myapp/public/javascripts
create : myapp/public/images
create : myapp/public/stylesheets
create : myapp/public/stylesheets/style.css
create : myapp/routes
create : myapp/routes/index.js
create : myapp/routes/users.js
create : myapp/views
create : myapp/views/index.pug
create : myapp/views/layout.pug
create : myapp/views/error.pug
create : myapp/bin
create : myapp/bin/www
$ npm install
$ npm start #navigate to localhost3000 to test everything works

This single command generates an app skeleton and gives you all the basic things you’ll need, run a quick npm install to make sure all the necessary packages have been installed. At this point fire up the server, using npm start, and navigate to localhost3000 to make sure everything’s installed correctly, you should see a message welcoming you to express.

All that’s left now is to add Mocha and Chai and you’re all set to go!

$ npm install mocha --save 
$ npm install chai --save

The ‘ — save’ option ensures the package is saved under the ‘dependancies’ section of your package.json, so when you run npm install, all of the necessary dependancies are installed. Once this is complete, you’re ready to start writing your first test, after you’ve made your ‘test’ directory and created your first spec file.

Integrating Travis

I’d recommend integrating Travis into any project you build, it gives you confidence that nothing is broken when merging work back into the master branch. It’s so simple and easy to setup, firstly you’ll need to create a .travis.yml file in the root of your project.

$ touch .travis.yml

Once this is done, open the file in your favourite text editor and add the following lines:

language: node_js
node_js:
- "node"

The lines are quiet self-explanatory and just let travis know what kind of project your building and the language it’s in.

When Travis builds your project, it runs the tests to check everything’s working and nothing is broken, it does this using the command ‘npm test’, this won’t work in our case because we’re using mocha to test our app, not the built-in testing library. Fear not, a very simple change in the package.json solves this problem.

{
"name": "myapp",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"test": "mocha"
},
"dependencies": {
"body-parser": "~1.15.2",
"cookie-parser": "~1.4.3",
"debug": "~2.2.0",
"express": "~4.14.0",
"morgan": "~1.7.0",
"pug": "~2.0.0-beta6",
"serve-favicon": "~2.3.0"
}
}

This is what your package.json file should look like, all you need to do is add the bolded line to the script (don’t forget the comma after the previous line or it won’t work) and when you now run npm test, it will run mocha.

Once this has been pushed, Travis will attempt to build and test your app, and as we have no tests, the build should pass with flying colours.

Thats all it takes really, now you’re ready to go and build your amazing app!

Software Developer https://jameshamann.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store