Git Product home page Git Product logo

loopback-modeltester's Introduction

loopback-modeltester

Loopback 3.x JSON - Model/Routes Unit testing

Installation

npm install loopback-modeltester --save

Usage example

Configure a JSON file with all use cases and routes to test :

[
  {
    "title": "FileStorage - upload Method",
    "method": "POST",
    "model": "filestorages",
    "url": "upload",
    "expect": {
      "statusCode": 200,
      "bodyType": "Object",
      "properties": {
        "id": "String"
      },
      "headers": {
        "content-type": "application/json"
      }
    },
    "file": {
      "path": "./test/SUService.log",
      "form_name": "fichier"
    },
    "variables": {
      "id": {
        "name": "uploadedImageId",
        "required": true
      }
    }
  },
  {
    "title": "FileStorage - getInfos Method",
    "model": "filestorages",
    "url": "getInfos/${uploadedImageId}",
    "expect": {
      "statusCode": 200,
      "bodyType": "Object"
    }
  },
  {
    "title": "FileStorage - listObjects Method",
    "model": "filestorages",
    "url": "listObjects",
    "expect": {
      "statusCode": 200,
      "bodyType": "Array"
    }
  },
  {
    "title": "FileStorage - download Method",
    "model": "filestorages",
    "url": "download/${uploadedImageId}",
    "debug": true,
    "expect": {
      "statusCode": 200,
      "headers": {
        "content-type": "text/plain"
      }
    }
  },
  {
    "title": "FileStorage - deleteByName Method",
    "method": "DELETE",
    "model": "filestorages",
    "url": "deleteByName/${uploadedImageId}",
    "expect": {
      "statusCode": 200,
      "bodyType": "Object",
      "properties": {
        "count": "Number"
      },
      "headers": {
        "content-type": "application/json"
      }
    }
  }
]

Create a test/test.js file at the root of your project with this content

'use strict';

// Setup manually dev env (setup the env here, or not if not required).
process.env.NODE_ENV = 'development';

// Require Packages
const loopbackModelTester = require('loopback-modeltester');
const app = require('../server/server');
const fileStorageTest = require('../server/model-test.json'); // Require your JSON here!

try {
  loopbackModelTester(app, fileStorageTest);
} catch (E) {
  console.error(E);
}

Documentation

For each tests, all followings keys are allowed. All fields based upon JavaScript types are checked with the lib @sindresorhus/is (So check this package to be sure you entered the right type).

Key Type Default Value Description
title String N.A Test title
skip Boolean false Skip or not the test
debug Boolean false Debug the rest by logging headers and body properties
method String GET The default HTTP Verbose method
model String N.A The model name in the plural form
url String N.A The request url
file Object N.A FormData to upload a file
expect Object N.A The expected response from the request
variables Object N.A Variables to assign to the context based upon the Body response

Expect properties

Key Type Default Value Description
statusCode Number 200 The expected HTTP status code
bodyType String N.A The expected body type (JavaScript)
headers Object N.A All headers key expected, with the value not matched explicitely
properties Object N.A All body properties expected

Properties values are JavaScript types..

Variables properties

Assign new variables into the context (to be used after with ${varName}). It useful when you want to create a chainable scenario.

Example

"variables": {
     "data[0]._id": {
          "required": true,
          "name": "user1_id"
     },
     "data[1]._id": {
          "required": true,
          "name": "user2_id"
     }
}

loopback-modeltester's People

Contributors

fraxken 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.