Loopback-modeltester
Loopback model unit testing that work with a JSON files
Getting Started
This package is available in the Node Package Repository and can be easily installed with npm or yarn.
$ npm i loopback-modeltester# or $ yarn add loopback-modeltester
The first step is to create a test/test.js file at the root of your project with the following content:
// Setup env to development (maybe required by your loopback configuration)processenvNODE_ENV = 'development'; // Require The packageconst loopbackTest = ; // Require Loopback server (app)const app = ; // Require one or multiple JSON that describe your tests!const modelTest = ; // Declare a new test ! ('api' is the REST base path of Loopback)const test = app 'api'; // Catch errortest; // Optionaly set a default payload for every tests!test; // Load your JSON test!test;
Find a complete JSON example in the root example repository.
API Example
Set a context variable before test execution
test;// Now use ${defaultUser} in your test!
Extend
Extend a specific test with some properties (it will merge every properties).
test;
And use it on your test by using the property extends
Before/After test execution
Execute action before or after the test by defining after or/and before AsyncFunction setter.
const test = app 'db' 'Collection';test { const colList = await DB; forconst col of colList ifcolname === 'system.indexes' continue; await DB; };
Note: DB Argument is only available when a defaultModel name has been given in the constructor
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 |
break | Boolean | false | Break or pause your 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 |
extends | Array | N.A | Array of extensions name |
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 |
form | Object | N.A | Request form (url-encoded).. |
formData | Object | N.A | Request formData |
variables | Array | 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 |
duration | Number | N.A | The expected baseline duration of the HTTP Request |
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 can be JavaScript types or an Object. Take the following example :
Variables
Variables assigned to the context can be used on different fields :
- URL
- Headers values
- Expected properties values
VSCode configuration
If you want JSON completion, you can configure intellisence with JSON Schema. Edit your configuration and put this:
"json.schemas": [
{
"fileMatch": [
"*.lb_tests.json"
],
"url": "./node_modules/loopback-modeltester/schema.json"
},
]