Skip to content

Make your Node Typescript unit testing a piece of cake.

Notifications You must be signed in to change notification settings

fluidtrends/savor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Savor Adds Delicious Flavors To Your Node Module, Like Tests, Coverage and Analysis.

Savor

Version Build Status Coverage Status Codacy Badge Author Tweet

Overview

Savor gives you all you need to write amazing tests, right out of the box, all in one place: a test framework, BDD and TDD assertions, stubbing/mocking, code coverage and static analysis.

Savor uses the following Open-Source libraries to make that happen:

  • Mocha as the test framework
  • Chai as the assertion library (both for BDD and for TDD)
  • Sinon as the stubbing library
  • Istanbul as the code coverage tool
  • ESLint as the static analyzer

Savor also gives you the ability to plug your tests into your continuous integration process via Coveralls for code coverage and Codacy for code analysis. To integrate with your CI tool, make sure you add a post-execution script that runs the following:

npm run coveralls
npm run codacy

For Travis CI for example, this is what your .travis.yml file might look like:

language: node_js
node_js:
  - "5.0"
after_success:
  - npm run coveralls
  - npm run codacy

Installation

STEP 1

Add Savor to your module as a development dependency:

npm install --save-dev savor

STEP 2

Add Savor to your module scripts:

"scripts": {
  "savor": "savor"
}

If you'd like more granularity over your scripts you can also install single Savor commands:

"scripts": {
  "savor": "savor",
  "test": "savor test",
  "lint": "savor lint",
  "cover": "savor cover",
  "coveralls": "savor coveralls",
  "codacy": "savor codacy"
}

STEP 3

Make sure your code resides under a src directory and all your specs under a test/specs directory:

package.json
node_modules/
src/
  main.js
test/
  assets/
    somefile.json
  specs/
    main.js

Adding Tests

You can now write tests under your test directory, like so:

var savor = require('savor');

savor.add('this is a test', function(context, done) {
  console.log('My test is running');

  // The test finished successfully
  done && done();

  // Or throw an error if the test fails
  // done && done(new Error('ddd'));
}).

// You can keep adding tests here with savor.add

run();

The Savor Context

When you add a test, you are given a context:

savor.add('this is a test', function(context, done) {
  ...
}).

This context contains the following:

  context.expect // Using Chai
  context.assert // Using Chai
  context.stub   // Using Sinon
  context.dir    // The temporary test location

Running Tests

You can now simply run your tests like so:

npm test

or like this:

npm run test

or even like this:

npm run savor test

Example

Test Coverage

You can check your coverage like this:

npm run coverage

or like this:

npm run savor coverage

Example

Static Analysis

You can lint your code like this:

npm run lint

or like this:

npm run savor lint

Example

Working Example

Take a look at the example for more details on how to integrate Savor within your module.

In our example, we have a simple module in src/main.js that generates a greeting, like so:

var main = {
  createGreeting: function(name) {
    return "Hello, " + name;
  }
}

module.exports = main;

And here's how it is to test this with Savor. First, add the Savor hooks in your scripts field:

"scripts": {
  "savor": "savor",
  "test": "savor test",
  "lint": "savor lint",
  "coverage": "savor coverage",
  "coveralls": "savor coveralls",
  "codacy": "savor codacy"
}

Next, write your test in test/main.js:

var savor = require('savor');
var main  = require('../src/main');

savor.add('should create a valid greeting', function(context, done) {
  var greeting = main.createGreeting('Dan');
  context.expect(greeting).to.equal("Hello, Dan");
  done && done();
}).

run('Greeting Tests');

Then simply run your tests, check your coverage and analyze your code as documented above.

Enjoy!

License

Copyright (c) 2016 I. Dan Calinescu

Licensed under the The MIT License (MIT) (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

https://raw.githubusercontent.com/idancali/savor/master/LICENSE

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.