Node Mocha Skeleton
NMS is a skeleton for building libraries in Node.JS. Clone the repo, and use it as a jumping-off point.
It demonstrates the approach I've gradually developed over the years for structuring my Node.JS libraries:
- it provides ready-to-run examples of the mocha testing framework.
- it demonstrates the usage of underscore, as a way to prettify array and object manipulation.
- it demonstrates the usage of optimist, a great library for simplifying the building of command-line applications.
Directory Structure
-
lib/ this is where all of your applications code will be stored. I approach my design in an OOP manner; hello.js contains a Hello class, banana.js a Banana class, etc.
-
test/ mocha will discover any tests that you place in this directory. There is generally a corresponding library file for each test, e.g., hello.js has a corresponding hello-test.js.
-
bin/ files within this folder expose a command-line-interface to your application. When npm install -g is run, these scripts will be installed globally.
-
package.json this file serves several purposes: it provides meta-information surrounding your project: author, version, description, etc; it describes the directory structure of your project; it describes your project's dependencies, (running npm install will install the dependencies described in package.json).
-
Makefile the make file is used to run the Mocha test framework. npm test looks for this Makefile, and uses it to execute Mocha's test suite.
-
README.md a great README file is an important part of any project, take some time to learn the ins and outs of markdown.
Library Dependencies
There are a few libraries that I pull into almost every Node.JS project that I begin:
-
mocha mocha is a light-weight testing framework for JavaScript. It provides both BDD and TDD interfaces, allowing you to choose whichever you feel more comfortable with.
-
underscore underscore provides functional programming constructs: map, reduce, extend, etc. It really helps to cleanup the logic surrounding array and object manipulation my applications.
-
optimist optimst is a simple, powerful, command-line-argument parser.
-
sinon sinon is a stand-alone testing framework. I use it specifically for its stubbing functionality.
Bootstrapping Your First Project with NMS
- Install the current stable version of Node.JS
- NPM is now packaged along with Node.JS, hooray!
- Clone this repo git clone git://github.com/bcoe/node-mocha-skeleton.git your-project-name
- Run npm install within the project directory.
- Run npm test.
- all tests should pass, if they don't:
- try to fix any obvious dependency issues, do you have make installed?
- is the problem my fault, create an issue on this project.
- Modify the skeleton project to your heart's content:
- remove the .git folder.
- run git init.
- add your new project as the origin, git remote add origin your-project-name.git
- edit the package.json, make it your own.
- Once you've built a killer library, make sure to publish it on NPM:
- npm adduser
- npm publish
-
npm update when you roll new versions.
Copyright
Copyright (c) 2013 Benjamin Coe. See LICENSE.txt for further details.