项目介绍

vlucas/frisby 是一个托管在 GitHub 上的开源项目,主要使用 JavaScript 编写,目前获得 1.5k 个 Star、196 次 Fork。

仓库全名
vlucas/frisby
Star 数
1.5k
Fork 数
196
未关闭 Issue
30

README 摘要

Frisby 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. Nested Dependent HTTP Calls A more complex example with nested dependent Frisby tests with Frisby's Promise-style then…

完整内容见 GitHub 原始 README 或本页 。

常见问题

frisby 用什么语言开发?

主要使用 JavaScript 编写,目前有 1.5k 个 Star。

如何获取 frisby 的源码?

可以直接克隆仓库:git clone https://github.com/vlucas/frisby.git,默认分支为 master;安装与运行步骤见仓库 README。

frisby 最近还在维护吗?

仓库最近一次更新为 1月8日,当前有 30 个未关闭 Issue;Star 数为 1.5k,可作为社区活跃度的参考。

关于

GitHub 原始描述(英文)

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

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

简要分析

热度分
1942
价值分
967
活跃状态
活跃

语言占比

JavaScript

README

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.

评论

暂无评论