• Stars
    star
    1,524
  • Rank 30,745 (Top 0.7 %)
  • Language
    JavaScript
  • Created over 12 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Frisby is a REST API testing framework built on Jest that makes testing API endpoints easy, fast, and fun.

Frisby

NPM CI

Frisby.js

Introduction

Frisby.js an API testing tool built on top of Jest that makes testing API endpoints easy, fast and fun.

Installation

Install Frisby v2.x from NPM into your project:

npm install --save-dev frisby joi

Creating Tests

Simple Example

The minimum setup to run a single test expectation.

const frisby = require('frisby');

it('should be a teapot', function () {
  // Return the Frisby.js Spec in the 'it()' (just like a promise)
  return frisby.get('http://httpbin.org/status/418')
    .expect('status', 418);
});

Nested Dependent HTTP Calls

A more complex example with nested dependent Frisby tests with Frisby's Promise-style then method.

const frisby = require('frisby');
const Joi = require('joi');

describe('Posts', function () {
  it('should return all posts and first post should have comments', function () {
    return frisby.get('http://jsonplaceholder.typicode.com/posts')
      .expect('status', 200)
      .expect('jsonTypes', '*', {
        userId: Joi.number(),
        id: Joi.number(),
        title: Joi.string(),
        body: Joi.string()
      })
      .then(function (res) { // res = FrisbyResponse object
        let postId = res.json[0].id;

        // Get first post's comments
        // RETURN the FrisbySpec object so function waits on it to finish - just like a Promise chain
        return frisby.get('http://jsonplaceholder.typicode.com/posts/' + postId + '/comments')
          .expect('status', 200)
          .expect('json', '*', {
            postId: postId
          })
          .expect('jsonTypes', '*', {
            postId: Joi.number(),
            id: Joi.number(),
            name: Joi.string(),
            email: Joi.string().email(),
            body: Joi.string()
          });
      });
  });
});

Built-In Expect Handlers

Frisby comes with many handy built-in expect handlers to help you test the HTTP response of your API.

  • status - Check HTTP status
  • header - Check HTTP header key + value
  • json - Match JSON structure + values (RegExp can be used)
  • jsonStrict - Match EXACT JSON structure + values (extra keys not tested for cause test failures)
  • jsonTypes - Match JSON structure + value types
  • jsonTypesStrict - Match EXACT JSON structure + value types (extra keys not tested for cause test failures)
  • bodyContains - Match partial body content (string or regex)
  • responseTime - Check if request completes within a specified duration (ms)

Define Custom Expect Handlers

When Frisby's built-in expect handlers are not enough, or if you find yourself running the same expectations in multiple places in your tests, you can define your own custom expect handler once, and then run it from anywhere in your tests.

beforeAll(function () {
  // Add our custom expect handler
  frisby.addExpectHandler('isUser1', function (response) {
    let json = response.body;

    // Run custom Jasmine matchers here
    expect(json.id).toBe(1);
    expect(json.email).toBe('[email protected]');
  });
});

// Use our new custom expect handler
it('should allow custom expect handlers to be registered and used', function () {
  return frisby.get('https://api.example.com/users/1')
    .expect('isUser1')
});

afterAll(function () {
  // Remove said custom handler (if needed)
  frisby.removeExpectHandler('isUser1');
});

Expecting JSON types using Joi

With Frisby, you can use Joi to set the expectation that the JSON body response from the HTTP call meets a defined schema. Check out the Joi API for more details.

Using Jasmine Matchers Directly

Any of the Jasmine matchers can be used inside the then method to perform additional or custom tests on the response data.

const frisby = require('frisby');

it('should be user 1', function () {
  return frisby.get('https://api.example.com/users/1')
    .then(function (res) {
      expect(res.json.id).toBe(1);
      expect(res.json.email).toBe('[email protected]');
    });
});

Running Tests

Frisby uses Jasmine style assertion syntax, and uses Jest to run tests.

Jest can run sandboxed tests in parallel, which fits the concept of HTTP testing very nicely so your tests run much faster.

Install Jest

npm install --save-dev jest

Create your tests

mkdir __tests__
touch __tests__/api.spec.js

Run your tests from the CLI

cd your/project
jest

Documentation

Documentation is hosted at frisbyjs.com, the documentation pages has separate repository.

License

Licensed under the BSD 3-Clause license.

More Repositories

1

phpdotenv

Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.
PHP
13,033
star
2

valitron

Valitron is a simple, elegant, stand-alone validation library with NO dependencies
PHP
1,567
star
3

bulletphp

A resource-oriented micro PHP framework
PHP
418
star
4

phpDataMapper

Object-Oriented PHP5 DataMapper ORM
PHP
118
star
5

pikirasa

PKI public/private RSA key encryption using the OpenSSL extension
PHP
102
star
6

sheetquery

Query Builder/ORM for Google Sheets
TypeScript
78
star
7

Spot

[DEPRECATED - use v2] Simple DataMapper ORM for PHP 5.3+
PHP
75
star
8

devdata.io

The Data You Need, The Programming Language You Want
HTML
75
star
9

vlid

Lightweight validation library with NO dependencies. A nice Joi alternative with a similar API.
JavaScript
65
star
10

universal-react-helloworld

Simplest possible starting point for using universal/isomorphic React.js + Node.js + Express.js
JavaScript
42
star
11

dos-css

DOS-Style CSS for retro fun
HTML
24
star
12

gasmask

Mocks for Google Apps Script libraries, specifically around Spreadsheets
TypeScript
22
star
13

hyperspan-php

Build a Hypermedia API response once and return it in multiple formats
PHP
18
star
14

toystore

Lightweight central store of state with the ability to watch for and react to specific property changes
JavaScript
16
star
15

bulletphp-skeleton

Skeleton example app using Bullet with templates and error handling
PHP
9
star
16

jsx-tmpl

Stop transpiling React components for Node. Use native ES6 template literals that output JSX instead!
JavaScript
8
star
17

turbolinks-demo-node

Copy of the Ruby Turbolinks Demo app in Node.js
HTML
6
star
18

echotag

Super simple ES6 tagged template function for printing an HTML string
JavaScript
6
star
19

toystore-react

React bindings for toystore (central store of state)
JavaScript
5
star
20

nudos

Heavily retro/DOS-inspired CSS while still modern enough to use
HTML
4
star
21

brightbudget-web

Demo web app API for a Hypermedia API presentation that powers brightbudget-app
JavaScript
4
star
22

presentation-slides-you-dont-know-nodejs

Presentation Slides for "You Don't Know Node.js"
CSS
4
star
23

jTreePlus

jTree with modifications to allow clickable links and form elements within tree nodes
JavaScript
3
star
24

jsx-native

Build JSX using native ES6 templates. No transpiling required for Node.js and modern browsers.
JavaScript
3
star
25

dotfiles-old

Personal dotfiles - mainly for ZSH and Vim configurations
Vim Script
3
star
26

phpunit-gearman-testing

Exploratory project on possible ways to test Gearman jobs using PHPUnit
PHP
3
star
27

brightbudget-app

Demo app for Hypermedia API presentation
JavaScript
3
star
28

spot-site

Spot DataMapper Website
HTML
2
star
29

example-browser-debugging

JavaScript
2
star
30

frisby-site

Main website for Frisby.js with documentation
2
star
31

startuporpharma.com

Is it a startup, or a pharmaceutical drug?
JavaScript
1
star
32

titanium-example-basic-counter

Example Titanium app with simple single-model binding to increment a counter
JavaScript
1
star
33

dotfiles-old-fresh

Vance's dotfiles managed by fresh
Shell
1
star
34

twicebreaker

Icebreaker game with the Twilio API
PHP
1
star
35

skycap

Drop-in authentication and authoriztion framework for Express.js
JavaScript
1
star
36

titemplate

Appcelerator Titanium template I use to start new projects - with coffeescript, Jade, a TSS reset, and other goodies
JavaScript
1
star
37

presentation-slides-js-browser-debugging

Presentation Slides for "Effective JavaScript Browser Debugging"
CSS
1
star
38

bulletphp-site

Main BulletPHP Website
PHP
1
star
39

budgetsheet-website-static-html

BudgetSheet Website
HTML
1
star
40

swagjs-site

Initial splash page signup site for JavaScript blocks
HTML
1
star
41

openx-oauth-client

Modern PHP client for working with the OpenX v4 oAuth API
PHP
1
star
42

titanium-chartjs-example

Example App with Chart.js Example in WebView
JavaScript
1
star
43

bulletphp-blog-example

Obligatory Blog Example built with the Bullet PHP Microframework
PHP
1
star