UNPKG

5.23 kBMarkdownView Raw
1# Frisby
2
3[![NPM](https://nodei.co/npm/frisby.png)](https://nodei.co/npm/frisby/)
4[![Build
5Status](https://travis-ci.org/vlucas/frisby.png?branch=master)](https://travis-ci.org/vlucas/frisby)
6
7![Frisby.js](https://www.frisbyjs.com/assets/frisbyjs.png)
8
9## Introduction
10
11Frisby.js an API testing tool built on top of
12[Jest](https://facebook.github.io/jest/) that makes testing API endpoints easy,
13fast and fun.
14
15## Installation
16
17Install Frisby v2.x from NPM into your project:
18
19 npm install frisby --save-dev
20
21## Creating Tests
22
23### Simple Example
24
25The minimum setup to run a single test expectation.
26
27```javascript
28const frisby = require('frisby');
29
30it('should be a teapot', function () {
31 // Return the Frisby.js Spec in the 'it()' (just like a promise)
32 return frisby.get('http://httpbin.org/status/418')
33 .expect('status', 418);
34});
35```
36
37### Nested Dependent HTTP Calls
38
39A more complex example with nested dependent Frisby tests with Frisby's Promise-style `then` method.
40
41```javascript
42const frisby = require('frisby');
43const Joi = frisby.Joi; // Frisby exposes Joi for convenience
44
45describe('Posts', function () {
46 it('should return all posts and first post should have comments', function () {
47 return frisby.get('http://jsonplaceholder.typicode.com/posts')
48 .expect('status', 200)
49 .expect('jsonTypes', '*', {
50 userId: Joi.number(),
51 id: Joi.number(),
52 title: Joi.string(),
53 body: Joi.string()
54 })
55 .then(function (res) { // res = FrisbyResponse object
56 let postId = res.json[0].id;
57
58 // Get first post's comments
59 // RETURN the FrisbySpec object so function waits on it to finish - just like a Promise chain
60 return frisby.get('http://jsonplaceholder.typicode.com/posts/' + postId + '/comments')
61 .expect('status', 200)
62 .expect('json', '*', {
63 postId: postId
64 })
65 .expect('jsonTypes', '*', {
66 postId: Joi.number(),
67 id: Joi.number(),
68 name: Joi.string(),
69 email: Joi.string().email(),
70 body: Joi.string()
71 });
72 });
73 });
74});
75```
76
77## Built-In Expect Handlers
78
79Frisby comes with many handy built-in expect handlers to help you test the HTTP
80response of your API.
81
82 * `status` - Check HTTP status
83 * `header` - Check HTTP header key + value
84 * `json` - Match JSON structure + values (RegExp can be used)
85 * `jsonStrict` - Match EXACT JSON structure + values (extra keys not tested for cause test failures)
86 * `jsonTypes` - Match JSON structure + value types
87 * `jsonTypesStrict` - Match EXACT JSON structure + value types (extra keys not tested for cause test failures)
88 * `bodyContains` - Match partial body content (string or regex)
89 * `responseTime` - Check if request completes within a specified duration (ms)
90
91## Define Custom Expect Handlers
92
93When Frisby's built-in expect handlers are not enough, or if you find yourself
94running the same expectations in multiple places in your tests, you can define
95your own custom expect handler once, and then run it from anywhere in your
96tests.
97
98```javascript
99beforeAll(function () {
100 // Add our custom expect handler
101 frisby.addExpectHandler('isUser1', function (response) {
102 let json = response.body;
103
104 // Run custom Jasmine matchers here
105 expect(json.id).toBe(1);
106 expect(json.email).toBe('testy.mctesterpants@example.com');
107 });
108});
109
110// Use our new custom expect handler
111it('should allow custom expect handlers to be registered and used', function () {
112 return frisby.get('https://api.example.com/users/1')
113 .expect('isUser1')
114});
115
116afterAll(function () {
117 // Remove said custom handler (if needed)
118 frisby.removeExpectHandler('isUser1');
119});
120```
121
122### Expecting JSON types using Joi
123
124With Frisby, you can use [Joi](https://github.com/hapijs/joi) to set the expectation that the JSON body response from the HTTP call meets a defined schema. Check out the [Joi API](https://github.com/hapijs/joi/blob/master/API.md) for more details.
125
126## Using Jasmine Matchers Directly
127
128Any of the [Jasmine matchers](http://jasmine.github.io/2.4/introduction.html)
129can be used inside the `then` method to perform additional or custom tests on
130the response data.
131
132```javascript
133const frisby = require('frisby');
134
135it('should be user 1', function () {
136 return frisby.get('https://api.example.com/users/1')
137 .then(function (res) {
138 expect(res.json.id).toBe(1);
139 expect(res.json.email).toBe('testy.mctesterpants@example.com');
140 });
141});
142```
143
144## Running Tests
145
146Frisby uses Jasmine style assertion syntax, and uses
147[Jest](https://facebook.github.io/jest/) to run tests.
148
149Jest can run sandboxed tests in parallel, which fits the concept of HTTP
150testing very nicely so your tests run much faster.
151
152### Install Jest
153
154 npm install --save-dev jest
155
156### Create your tests
157
158 mkdir __tests__
159 touch __tests__/api.spec.js
160
161### Run your tests from the CLI
162
163 cd your/project
164 jest
165
166### Documentation
167
168Documentation is hosted at [frisbyjs.com](http://frisbyjs.com/), the
169documentation pages has separate
170[repository](https://github.com/vlucas/frisby-site).
171
172## License
173
174Licensed under the [BSD 3-Clause](http://opensource.org/licenses/BSD-3-Clause)
175license.