3.0.0 • Published 5 years ago

loopback-modeltester v3.0.0

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

Loopback-modeltester

Maintenance GitHub 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 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)
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

test.setVar('defaultUser', 'GENTILHOMME Thomas');
// Now use ${defaultUser} in your test!

Extend

Extend a specific test with some properties (it will merge every properties).

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.

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

KeyTypeDefault ValueDescription
titleStringN.ATest title
skipBooleanfalseSkip or not the test
breakBooleanfalseBreak or pause your test
debugBooleanfalseDebug the rest by logging headers and body properties
methodStringGETThe default HTTP Verbose method
modelStringN.AThe model name in the plural form
extendsArrayN.AArray of extensions name
urlStringN.AThe request url
fileObjectN.AFormData to upload a file
expectObjectN.AThe expected response from the request
formObjectN.ARequest form (url-encoded)..
formDataObjectN.ARequest formData
variablesArrayN.AVariables to assign to the context based upon the Body response

Expect properties

KeyTypeDefault ValueDescription
statusCodeNumber200The expected HTTP status code
durationNumberN.AThe expected baseline duration of the HTTP Request
bodyTypeStringN.AThe expected body type (JavaScript)
headersObjectN.AAll headers key expected, with the value not matched explicitely
propertiesObjectN.AAll body properties expected

Properties values can be JavaScript types or an Object. Take the following example :

{
  "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"
    },
]
3.0.0

5 years ago

2.9.0

6 years ago

2.8.1

6 years ago

2.8.0

6 years ago

2.7.1

6 years ago

2.7.0

6 years ago

2.6.1

6 years ago

2.6.0

6 years ago

2.5.2

6 years ago

2.5.1

6 years ago

2.5.0

6 years ago

2.4.0

6 years ago

2.3.4

6 years ago

2.3.3

6 years ago

2.3.2

6 years ago

2.3.1

6 years ago

2.3.0

6 years ago

2.2.0

6 years ago

2.1.0

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.6.0

6 years ago

1.5.1

6 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago