# loopback-modeltester

> Loopback 3.X - Routes and Models unit testing

Latest version **3.0.0** (published 2019-09-13) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install loopback-modeltester
pnpm add loopback-modeltester
yarn add loopback-modeltester
bun add loopback-modeltester
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2019-09-13 |
| First published | 2017-11-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=10 |
| Dependencies | 5 |
| Unpacked size | 26.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | GENTILHOMME Thomas |
| Maintainers | fraxken |

## Links

- npm: https://www.npmjs.com/package/loopback-modeltester
- Repository: https://github.com/fraxken/loopback-modeltester
- Homepage: https://github.com/fraxken/loopback-modeltester#readme
- Issues: https://github.com/fraxken/loopback-modeltester/issues
- npm.io page: https://npm.io/package/loopback-modeltester

## Dependencies (5)

- [chalk](https://npm.io/package/chalk.md) ^2.3.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.4
- [@slimio/is](https://npm.io/package/@slimio/is.md) ^1.5.1
- [mime-types](https://npm.io/package/mime-types.md) ^2.1.17
- [request-promise](https://npm.io/package/request-promise.md) ^4.2.2

## Recent versions

- 3.0.0 (latest) — 2019-09-13
- 2.9.0 — 2018-07-27
- 2.8.1 — 2018-03-01
- 2.8.0 — 2018-03-01
- 2.7.1 — 2018-02-27
- 2.7.0 — 2018-02-27
- 2.6.1 — 2018-02-20
- 2.6.0 — 2018-02-20
- 2.5.2 — 2018-02-02
- 2.5.1 — 2018-01-31
- 2.5.0 — 2018-01-31
- 2.4.0 — 2018-01-30
- 2.3.4 — 2018-01-29
- 2.3.3 — 2018-01-29
- 2.3.2 — 2018-01-29
- … 19 more at https://npm.io/package/loopback-modeltester/versions

## README

# Loopback-modeltester
[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/fraxken/loopback-modeltester/graphs/commit-activity)
[![GitHub license](https://img.shields.io/github/license/Naereen/StrapDown.js.svg)](https://github.com/fraxken/loopback-modeltester/blob/master/LICENSE)

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](https://docs.npmjs.com/getting-started/what-is-npm) or [yarn](https://yarnpkg.com).

```bash
$ 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:

```js
// Setup env to development (maybe required by your loopback configuration)
process.env.NODE_ENV = 'development';

// Require The package
const loopbackTest = require('loopback-modeltester');

// Require Loopback server (app)
const app = require('../server/server');

// Require one or multiple JSON that describe your tests!
const modelTest = require('./model-tests.json');

// Declare a new test ! ('api' is the REST base path of Loopback)
const test = new loopbackTest(app, 'api');

// Catch error
test.on('error', console.error);

// Optionaly set a default payload for every tests!
test.defaultPayload({
    headers: {
        foo: 'bar'
    }
});

// Load your JSON test!
test.load(modelTest);
```

Find a complete JSON example in the root example repository.

## API Example

### Set a context variable before test execution

```js
test.setVar('defaultUser', 'GENTILHOMME Thomas');
// Now use ${defaultUser} in your test!
```

### Extend

Extend a specific test with some properties (it will merge every properties).

```js
test.extend('customPayload01', {
    expect: {
        statusCode: 500,
        duration: 100,
        bodyType: 'Object',
        properties: {
            error: {
                type: 'String',
                value: 'An error occured on the Server!'
            }
        }
    }
});
```
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.

```js
const test = new loopbackTest(app, 'db', 'Collection');
test.after = async function deleteCols(DB) {
	const colList = await DB.listCollections().toArray();
	for(const col of colList) {
		if(col.name === 'system.indexes') {
			continue;
		}
		await DB.dropCollection(col.name);
	}
};
```

> **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<String> | 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 : 

```json
{
  "error": "null",
  "insertedCount": {
    "type": "Number",
    "value": 1
  },
  "insertedId": "String"
}
```

### 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"
    },
]
```

---
_Source: https://npm.io/package/loopback-modeltester · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
