Coder Social home page Coder Social logo

renanpallin / frisby Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vlucas/frisby

0.0 2.0 0.0 609 KB

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

Home Page: http://frisbyjs.com

JavaScript 100.00%

frisby's Introduction

Frisby

NPM Build Status

Frisby.js

Sponsored by Okta Sponsored by Okta

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 frisby --save-dev

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 = frisby.Joi; // Frisby exposes Joi for convenience

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)

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');
});

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.

frisby's People

Contributors

vlucas avatar h1gdev avatar kmorey avatar marcin-wosinek avatar christianscheja avatar ericboehs avatar m0x72 avatar renanpallin avatar koooge avatar cjsaylor avatar plpeeters avatar donatj avatar sudodoki avatar extronics avatar dbashford avatar danactive avatar valentinh avatar jturmel avatar davewid avatar petrjasek avatar psaxton avatar rblu avatar slicknutter avatar sri85 avatar tleyden avatar darrenxyli-aa avatar derekhe avatar kreutter avatar marcoscarceles avatar sasikanth avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.