UNPKG

6.75 kBMarkdownView Raw
1<p align="center">
2 <a href="https://github.com/idancali/savor">
3 <img height="256" src="https://raw.githubusercontent.com/idancali/savor/master/logo.png">
4 </a>
5 <p align="center"> <b> Savor </b> Adds Delicious Flavors To Your Node Module, Like Tests, Coverage and Analysis. </p>
6</p>
7
8# Savor
9
10[![Version](https://img.shields.io/npm/v/savor.svg)](https://www.npmjs.com/package/savor)
11[![Build Status](https://travis-ci.org/idancali/savor.svg?branch=master)](https://travis-ci.org/idancali/savor)
12[![CC](https://codeclimate.com/github/idancali/savor/badges/gpa.svg)](https://codeclimate.com/github/idancali/savor)
13[![TC](https://codeclimate.com/github/idancali/savor/badges/coverage.svg)](https://codeclimate.com/github/idancali/savor)
14[![Author](https://img.shields.io/badge/say%20hi-%40idancali-green.svg)](https://twitter.com/idancali)
15[![Tweet](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?url=https%3A%2F%2Fgithub.com%2Fidancali%2Fsavor&via=idancali&text=Add%20more%20flavor%20to%20your%20Node%20module%20%28test%2C%20coverage%2C%20analysis%29.&hashtags=savor%2C%20opensource&)
16
17# Overview
18
19Savor 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.
20
21Savor uses the following Open-Source libraries to make that happen:
22
23 - [Mocha](https://mochajs.org) as the test framework
24 - [Chai](http://chaijs.com) as the assertion library (both for BDD and for TDD)
25 - [Sinon](http://sinonjs.org) as the stubbing library
26 - [Istanbul](http://gotwarlost.github.io/istanbul) as the code coverage tool
27 - [ESLint](http://eslint.org) as the static analyzer
28
29Savor also gives you the ability to plug your tests into your continuous integration process via [Coveralls](https://coveralls.io) for code coverage and [Codacy](https://www.codacy.com) for code analysis. It also support [CodeClimate](https://codeclimate.com) which is a tool that offers both static analysis and code coverage. To integrate with your CI tool, make sure you add a post-execution script that runs the following:
30```
31npm run coveralls
32npm run codacy
33npm run codeclimate
34```
35
36For [Travis CI](https://travis-ci.org) for example, this is what your ```.travis.yml``` file might look like:
37```yaml
38language: node_js
39node_js:
40 - "5.0"
41after_success:
42 - npm run coveralls
43 - npm run codacy
44 - npm run codeclimate
45```
46
47# Installation
48
49**STEP 1**
50
51Add Savor to your module as a development dependency:
52
53```javascript
54npm install --save-dev savor
55```
56
57**STEP 2**
58
59Add Savor to your module scripts:
60
61```javascript
62"scripts": {
63 "savor": "savor"
64}
65```
66
67If you'd like more granularity over your scripts you can also install single Savor commands:
68
69```javascript
70"scripts": {
71 "savor": "savor",
72 "test": "savor test",
73 "lint": "savor lint",
74 "cover": "savor cover",
75 "coveralls": "savor coveralls",
76 "codacy": "savor codacy"
77 "codeclimate": "savor codeclimate"
78}
79```
80
81**STEP 3**
82
83Make sure your code resides under a ```src``` directory and all your specs under a ```test/specs``` directory:
84
85```javascript
86package.json
87node_modules/
88src/
89 main.js
90test/
91 assets/
92 somefile.json
93 specs/
94 main.js
95```
96
97# Adding Tests
98
99You can now write tests under your ```test``` directory, like so:
100
101```javascript
102var savor = require('savor');
103
104savor.add('this is a test', function(context, done) {
105 console.log('My test is running');
106
107 // The test finished successfully
108 done && done();
109
110 // Or throw an error if the test fails
111 // done && done(new Error('oops'));
112}).
113
114// You can keep adding tests here with savor.add
115
116run();
117```
118
119# The Savor Context
120
121When you add a test, you are given a ```context```:
122
123```javascript
124savor.add('this is a test', function(context, done) {
125 ...
126}).
127```
128
129This ```context``` contains the following:
130
131```javascript
132 context.expect // Using Chai
133 context.assert // Using Chai
134 context.stub // Using Sinon
135 context.shallow // Using Enzyme
136 context.mount // Using Enzyme
137 context.render // Using Enzyme
138 context.dir // The temporary test location
139```
140
141# Running Tests
142
143You can now simply run your tests like so:
144
145```javascript
146npm test
147```
148
149or like this:
150
151```javascript
152npm run test
153```
154
155or even like this:
156
157```javascript
158npm run savor test
159```
160
161![Example](https://raw.githubusercontent.com/idancali/savor/master/examples/example.main.1.gif)
162
163# Test Coverage
164
165You can check your coverage like this:
166
167```javascript
168npm run coverage
169```
170
171or like this:
172
173```javascript
174npm run savor coverage
175```
176
177![Example](https://raw.githubusercontent.com/idancali/savor/master/examples/example.main.3.gif)
178
179# Static Analysis
180
181You can lint your code like this:
182
183```javascript
184npm run lint
185```
186
187or like this:
188
189```javascript
190npm run savor lint
191```
192
193![Example](https://raw.githubusercontent.com/idancali/savor/master/examples/example.main.2.gif)
194
195# Working Example
196
197[Take a look at the example](https://github.com/idancali/savor/tree/master/examples/main) for more details on how to integrate Savor within your module.
198
199In our example, we have a simple module in ```src/main.js``` that generates a greeting, like so:
200
201```javascript
202var main = {
203 createGreeting: function(name) {
204 return "Hello, " + name;
205 }
206}
207
208module.exports = main;
209```
210
211And here's how it is to test this with Savor. First, add the Savor hooks in your ```scripts``` field:
212
213```javascript
214"scripts": {
215 "savor": "savor",
216 "test": "savor test",
217 "lint": "savor lint",
218 "coverage": "savor coverage",
219 "coveralls": "savor coveralls",
220 "codacy": "savor codacy",
221 "codeclimate": "savor codeclimate"
222}
223```
224
225Next, write your test in ```test/main.js```:
226
227```javascript
228var savor = require('savor');
229var main = require('../src/main');
230
231savor.add('should create a valid greeting', function(context, done) {
232 var greeting = main.createGreeting('Dan');
233 context.expect(greeting).to.equal("Hello, Dan");
234 done && done();
235}).
236
237run('Greeting Tests');
238```
239
240Then simply [run your tests](#running-tests), [check your coverage](#test-coverage) and [analyze your code](#static-analysis) as documented above.
241
242Enjoy!
243
244# License
245
246Copyright (c) 2016 I. Dan Calinescu
247
248 Licensed under the The MIT License (MIT) (the "License");
249 you may not use this file except in compliance with the License.
250 You may obtain a copy of the License at
251
252 https://raw.githubusercontent.com/idancali/savor/master/LICENSE
253
254 Unless required by applicable law or agreed to in writing, software
255 distributed under the License is distributed on an "AS IS" BASIS,
256 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
257 See the License for the specific language governing permissions and
258 limitations under the License.