just-api is a free, open source api development & testing project written in JavaScript and released under MIT. It has 814 GitHub stars, 36 forks and 19 open issues, and was last pushed 7 months ago. On this registry it ranks #94 of 103 tracked projects in API Development & Testing, with 5 head-to-head comparisons available.

What is just-api?

What it is

Just-API is a declarative, specification-based test framework for REST and GraphQL APIs, written in JavaScript and distributed under the MIT license. It lives in the Node.js ecosystem and runs from the npm package just-api. Instead of writing test code, a user describes a request and the response validation rules in a YAML file, and the framework builds the request, sends it to the server, and checks the response against that specification. Each suite can contain one or more specs, and suites run in serial or parallel mode.

The concrete problem it solves is the boilerplate that normally surrounds API testing. Validating a status code, a header, a cookie, a JSON body, or a JSON schema usually means writing and maintaining imperative test code, and chained flows that depend on data fetched by an earlier request make that code harder still. Just-API reads the specification from a plain YAML file and performs the validation itself, so users can test APIs without writing code, while still tapping into JavaScript when they need custom logic. It also supports chained request flows, hooks, and intrasuite and intersuite spec dependencies, which covers flows that are awkward to express in flat test files.

Key capabilities

  • Runs test suites in parallel or serial mode.
  • Supports all widely used HTTP methods, plus x-www-form-urlencoded requests, multipart requests, and file uploads.
  • Built-in response validation constructs for status code, headers, cookies, JSON body, and JSON schema, with custom response validator functions using sync or async JavaScript.
  • Hooks for Before All, After All, Before Each, After Each, Before Test, and After Test, plus custom suite configuration.
  • Chained request flows with the ability to define or override request path, query params, path params, headers, and body at runtime.
  • Built-in HTML and JSON reporters, multiple report formats for the same run, plug-in custom reporters, and logging of HTTP request and response data for failed tests.
  • Retry of failed tests, looping to generate n tests from a list, pattern-based test selection, skipping tests by specification, and enable or disable of redirections.

Who uses it and how

  • Teams testing microservices and web services whose APIs are already described by request and response shapes rather than by test code.
  • Developers running REST suites against services such as swapi.co, validating status, headers, and JSON body values declared in YAML.
  • Developers testing GraphQL endpoints by posting a query payload and asserting on the returned JSON data, as in the Star Wars character example.
  • Engineers building chained flows where a hook fetches prerequisite data and passes it into the actual test through suite and test context.
  • Users importing specs from one or more suites, reusing specifications, and sharing results through custom reporters.

Getting started

The README requires Node.js v10.x.x or newer and installs with npm install just-api. Suites are written

project readme (upstream, from github) — read inline

Just-API

npm package

Just-API is a declarative, specification based test framework for REST, GraphQL APIs. Users can test APIs without writing code, but they can also tap into code when they want to. It reads API test specification from YAML files and runs them in serial/parallel mode. Test reports can be generated in several formats including HTML and JSON.

In simple terms, users build a test suite by providing a set of request and response validation specification in a YAML file. Each suite can have one or more specs. Just-API builds the request, sends it to server and validates response as per the specification. One can choose to validate any or all of following

  • Status code
  • Headers
  • Cookies
  • Response JSON body
  • Response JSON schema

or Provide a custom Javascript function to validate the response

Find more here

Codebase visualization diagram

Codebase as a diagram

Links

Features

  • Runs test suites in parallel/serial mode
  • Supports all widely used HTTP methods
  • Supports x-www-form-urlencoded requests, Multipart requests, File uploads
  • Built-in Response Validation Constructs(Headers, Cookies, Status code, JSON body, JSON schema)
  • Custom Response validator functions
  • Supports running custom inline or module javascript sync/async functions
  • Supports Hooks (Before All, After All, Before Each, After Each, Before Test, After Test)
  • Custom suite configuration
  • Chained Request flows
  • Define/override Request path, query params, path params, headers, body at runtime
  • Suite and test context for reuse
  • Supports importing specs from one or more test suites
  • Intrasuite and Intersuite spec dependencies
  • Reusing test specification
  • Retry failed tests
  • Looping: Generate 'n' number of tests with a list
  • Built-in HTML, JSON reporters
  • Can generate reports in multiple formats for the same run
  • Logging HTTP request/response data for failed tests
  • Proper error reporting
  • Can run tests matching with a given pattern/string
  • Skipping tests with specification
  • Disable or Enable redirections
  • Reports test duration
  • Allows user to plug-in custom reporters

See all features

Getting Started

To run just-api, you will need Node.js v10.x.x or newer.

Installation

$ npm install just-api

Following is a simple example showing usage of Just-API.

$ mkdir specs
$ vim specs/starwars_service.yml

Write following suite in your editor

meta:
  name: Star Wars suite
configuration:
  scheme: https
  host: swapi.co
  base_path: /api
specs:
  - name: get Luke Skywalker info
    request:
      path: /people/1/
      method: get
    response:
      status_code: 200
      headers:
        - name: content-type
          value: !!js/regexp application/json     
      json_data:
        - path: $.name
          value: Luke Skywalker

Back in the terminal

$ ./node_modules/.bin/just-api

   ✓ get Luke Skywalker info (1216ms)

  Done: specs/starwars_service.yml (Passed)

0 skipped, 0 failed, 1 passed (1 tests)
0 skipped, 0 failed, 1 passed (1 suites)
Duration: 1.3s

Testing GraphQL APIs

Following example tests a GraphQL API that returns Person info for a given name.

Create a YAML suite and run just-api.

meta:
  name: GraphQL Starwars service
configuration:
  host: swapi.graph.cool
  scheme: https
specs:
  - name: Get Details of a character
    request:
      method: post
      headers:
        - name: content-type
          value: application/json
      payload:
        body:
          type: json
          content:
            query: >
                   {
                    Person(name: "Luke Skywalker") {
                      name,
                      id,
                      gender
                     }
                    }
            variables: null
            operationName: null
    response:
      status_code: 200
      json_data:
        - path: $.data.Person.name
          value: "Luke Skywalker"

A chained request flow with hook and custom validation

When you need to test complex chained API flows, run dependencies in hooks to fetch pre-requisite data and pass it to actual test.

Following example shows how to run dependencies using a hook, get data and validating response with a custom validator function.

meta:
  name: Starwars suite
configuration:
  scheme: https
  host: swapi.co
  base_path: /api
specs:
  - name: get R2-D2 info
    request:
      path: /people/3/
      method: get
    response:
      status_code: 200
      json_data:
        - path: $.name
          value: R2-D2

  - name: search R2-D2 info
    before_test:
      run_type: inline
      inline:
        function: !js/asyncFunction >
          async function() {
            var response = await this.runSpec('get R2-D2 info');
            var jsonData = JSON.parse(response.body);
            this.test.query_params = { name:  jsonData.name };
          }
    request:
      path: /people
      method: get
    response:
      status_code: 200
      custom_validator:
        run_type: inline
        inline:
          function: !!js/function >
            function() {
              var jsonData = JSON.parse(this.response.body);
              var r2d2 = jsonData.results.find(result => result.name === 'R2-D2');

              if (!r2d2)
                throw new Error('R2-D2 not returned in search results');
            }

Note: You can also place custom JS functions in a module and specify the function name, module path in YAML to import.

More advanced stuff can be done with Just-API. Documentation says it all. Take a look at Just-API Website for detailed documentation.

If you are looking to use Docker to run Just-API, you might want to checkout Just-API docker boilerplate here

Maintainer

Kiran [email protected]

License

MIT-licensed

References

Donation

If this project helps you in anyway, Please consider making a donation

Contributing

NOTE: recommend Node v10.x since v12.x has gulp compatibility issue.

  1. Install deps npm install
  2. Install gulp npm install -g gulp
  3. Install test files gulp
  4. Install test API npm run install_testapi
  5. Run test API npm run start_testapi
  6. (in a new window) npm test

Test Structure

  • test/cli/src/suites/[suite].spec.yaml contains sample suites/specs
  • test/cli/[suite].spec.js contains JS chai/mocha test assertions about the sample suite/specs

You may need to create/modify both a sample suite/spec and corresponding JS assertion

Code Quality & Formatting

TODO: add linter/hinter/prettier or whatever spec is used

Community

Join the chat at https://gitter.im/just-api/Lobby Twitter

Frequently asked questions

Is just-api free to use?

just-api is open source under the MIT licence. There is no licence fee and no seat count — you can self-host it or, where the project offers one, pay a vendor for a managed version instead.

What does just-api do?

:boom: Test REST, GraphQL APIs

What is just-api written in?

just-api is primarily written in JavaScript. Its source is publicly available at https://github.com/kiranz/just-api, and it has 814 GitHub stars.