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 |
|
19 | 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.
|
20 |
|
21 | Savor 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 |
|
29 | Savor 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 | ```
|
31 | npm run coveralls
|
32 | npm run codacy
|
33 | npm run codeclimate
|
34 | ```
|
35 |
|
36 | For [Travis CI](https://travis-ci.org) for example, this is what your ```.travis.yml``` file might look like:
|
37 | ```yaml
|
38 | language: node_js
|
39 | node_js:
|
40 | - "5.0"
|
41 | after_success:
|
42 | - npm run coveralls
|
43 | - npm run codacy
|
44 | - npm run codeclimate
|
45 | ```
|
46 |
|
47 | # Installation
|
48 |
|
49 | **STEP 1**
|
50 |
|
51 | Add Savor to your module as a development dependency:
|
52 |
|
53 | ```javascript
|
54 | npm install --save-dev savor
|
55 | ```
|
56 |
|
57 | **STEP 2**
|
58 |
|
59 | Add Savor to your module scripts:
|
60 |
|
61 | ```javascript
|
62 | "scripts": {
|
63 | "savor": "savor"
|
64 | }
|
65 | ```
|
66 |
|
67 | If 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 |
|
83 | Make sure your code resides under a ```src``` directory and all your specs under a ```test/specs``` directory:
|
84 |
|
85 | ```javascript
|
86 | package.json
|
87 | node_modules/
|
88 | src/
|
89 | main.js
|
90 | test/
|
91 | assets/
|
92 | somefile.json
|
93 | specs/
|
94 | main.js
|
95 | ```
|
96 |
|
97 | # Adding Tests
|
98 |
|
99 | You can now write tests under your ```test``` directory, like so:
|
100 |
|
101 | ```javascript
|
102 | var savor = require('savor');
|
103 |
|
104 | savor.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 |
|
116 | run();
|
117 | ```
|
118 |
|
119 | # The Savor Context
|
120 |
|
121 | When you add a test, you are given a ```context```:
|
122 |
|
123 | ```javascript
|
124 | savor.add('this is a test', function(context, done) {
|
125 | ...
|
126 | }).
|
127 | ```
|
128 |
|
129 | This ```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 |
|
143 | You can now simply run your tests like so:
|
144 |
|
145 | ```javascript
|
146 | npm test
|
147 | ```
|
148 |
|
149 | or like this:
|
150 |
|
151 | ```javascript
|
152 | npm run test
|
153 | ```
|
154 |
|
155 | or even like this:
|
156 |
|
157 | ```javascript
|
158 | npm run savor test
|
159 | ```
|
160 |
|
161 | ![Example](https://raw.githubusercontent.com/idancali/savor/master/examples/example.main.1.gif)
|
162 |
|
163 | # Test Coverage
|
164 |
|
165 | You can check your coverage like this:
|
166 |
|
167 | ```javascript
|
168 | npm run coverage
|
169 | ```
|
170 |
|
171 | or like this:
|
172 |
|
173 | ```javascript
|
174 | npm run savor coverage
|
175 | ```
|
176 |
|
177 | ![Example](https://raw.githubusercontent.com/idancali/savor/master/examples/example.main.3.gif)
|
178 |
|
179 | # Static Analysis
|
180 |
|
181 | You can lint your code like this:
|
182 |
|
183 | ```javascript
|
184 | npm run lint
|
185 | ```
|
186 |
|
187 | or like this:
|
188 |
|
189 | ```javascript
|
190 | npm 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 |
|
199 | In our example, we have a simple module in ```src/main.js``` that generates a greeting, like so:
|
200 |
|
201 | ```javascript
|
202 | var main = {
|
203 | createGreeting: function(name) {
|
204 | return "Hello, " + name;
|
205 | }
|
206 | }
|
207 |
|
208 | module.exports = main;
|
209 | ```
|
210 |
|
211 | And 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 |
|
225 | Next, write your test in ```test/main.js```:
|
226 |
|
227 | ```javascript
|
228 | var savor = require('savor');
|
229 | var main = require('../src/main');
|
230 |
|
231 | savor.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 |
|
237 | run('Greeting Tests');
|
238 | ```
|
239 |
|
240 | Then simply [run your tests](#running-tests), [check your coverage](#test-coverage) and [analyze your code](#static-analysis) as documented above.
|
241 |
|
242 | Enjoy!
|
243 |
|
244 | # License
|
245 |
|
246 | Copyright (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.
|