Skip to main content

API Reference

Each method can be deconstructed from the object returned by require('@restqa/restqa').


Install(options)

Generate a configuration file output from an add on install.

  • name (string) : The name of the addon you want to install (ex: 'slack')
  • configFile (string) : Location of the RestQA Configuration File (ex: './restqa.yml')
  • env (string) : The target environment (from your RestQA config file) (ex: 'local')
  • config (object) : Represent the configuration required to setup the addon

Example:

const { Install } = require('@restqa/restqa')
const opt = {
  name: 'slack',
  env: 'production',
  configFile: './.restqa.yml',
  config: {
    url: 'https://slack.com/incoming-webhook'
  }
}
const result = Install(opt)
console.log(result)

Output:

version: 0.0.1
metadata:
  code: APP
  name: app
  description: Configuration generated by restqa init -y
environments:
  - name: production
    default: true
    plugins:
      - name: restqapi
        config:
          url: https://api.restqa.io
    outputs:
      - type: http-html-report
        enabled: true
      - type: file
        enabled: true
        config:
          path: restqa-result.json
      - type: slack
        enabled: true
        config:
          url: hhttps://slack.com/incoming-webhook
          onlyFailed: false

This method is printing the output. It will not override your current configuration file.

Generate(cmd)

To generate a scenario from a curl command, the process will use the parameter from the curl command to call your server then catch the response and generate a valid RestQA Test Scenario (Gherkin format)

Example:

const { Generate } = require('@restqa/restqa')

async function main() {
  let result = await Generate('curl https://jsonplaceholder.typicode.com/todos/1')
  console.log(result)
}

main()

Output:

Given I have the api gateway hosted on "https://jsonplaceholder.typicode.com"
  And I have the path "/todos/1"
  And I have the method "GET"
When I run the API
Then I should receive a response with the status 200
  And the response body should be equal to:
  """
{
  "userId": 1,
  "id": 1,
  "title": "delectus aut autem",
  "completed": false
}
  """

Steps(options)

Retrieve the list of step definition available for a specific file.

  • keyword (string) : Step definition keyword (ex: 'given')
  • config (string) : Specify the configuration file to use (ex: './restqa.yml')
  • tag (string) : Filter the step to a specific keyword (ex: 'cookie')

Example:

const { Steps } = require('@restqa/restqa')
const opt = {
  keyword: 'then',
  configFile: './.restqa.yml',
  tag: 'header',
}
const result = Steps(opt)
console.log(result)

Output:

[
  {
    Plugin: '@restqa/restqapi',
    Keyword: 'then',
    Step: 'I should receive a response with the status {int}',
    Comment: 'Check the response http code'
  },
  {
    Plugin: '@restqa/restqapi',
    Keyword: 'then',
    Step: 'the response time is under {int} ms',
    Comment: 'Check the response latency'
  },
  {
    Plugin: '@restqa/restqapi',
    Keyword: 'then',
    Step: 'the header {string} should be {string}',
    Comment: 'Check if a property in the response header has the exact string value'
  },
...

Run(options)

Run the test suite

  • configFile (string): Specify the configuration file to use (ex: './restqa.yml')
  • env (string): Define the target environemnt
  • stream (Stream): Write stream (default: stdOut)
  • path (string): Path folder of the test scenario

Example:

const { Run } = require('@restqa/restqa')
const Stream = require('stream')

const opt = {
  configFile: '.restqa.yml',
  env: 'local',
  stream: new Stream.Writable()
}
  
Run(opt)
  .then(result => {
    console.log(result)
  })
  .catch(err => {
    console.log(err)
  })

Output:

{
  id: 'c28812ec-64ed-4f8d-9538-f5d4f6be9454',
  startTime: '2021-03-12T09:39:12+00:00',
  name: 'app',
  key: 'APP',
  env: 'local',
  duration: 0.858,
  success: false,
  durationFormat: '00:00',
...

Dashboard(options)

Expose the RestQA Dashboard using a specific configuration

  • configFile (string): Specify the configuration file to use (ex: './restqa.yml')
  • folder (string): Define the folder where to project is located
  • readOnly (boolean): Restrict the access to the feature file into read only

Example:

const { Dashboard } = require('@restqa/restqa')

const opt = {
  configFile: '.restqa.yml',
  folder: './example',
  readOnly: true
}
  
const server = Dashboard(opt)
server.listen(8000, () => {
  console.log('The dashboard is running on the port 8000')
})

Hooks

Expose the RestQA Dashboard within your microservice

Express

  • configFile (string): Specify the configuration file to use (ex: './restqa.yml')
  • readOnly (boolean): Restrict the access to the feature file as read only
  • route (boolean): Route used to exposed the dashboard (default: '/restqa')

Example:

const express = require('express')
const { Hooks } = require('@restqa/restqa')

const opt = {
  configFile: __dirname + '/.restqa.yml',
  readOnly: false,
  folder: __dirname +_ '/tests',
  route: '/restqa'
};

const app = express()

Hooks.express(app, opt)

app
  .get('/', (req, res) => res.json({ message: 'Welcome'}))
  .listen(8082, () => console.log('Server running on the host http://localhost:8082'))

Fastify

Coming soon :)