Page Contents

Home > @loopback/testlab

testlab package

A collection of test utilities we use to write LoopBack tests.

Remarks

Test utilities to help write LoopBack 4 tests:

  • expect - behavior-driven development (BDD) style assertions - sinon - test spies: functions recording arguments and other information for all of their calls - stubs: functions (spies) with pre-programmed behavior - mocks: fake methods (like spies) with pre-programmed behavior (like stubs) as well as pre-programmed expectations - Helpers for creating supertest clients for LoopBack applications - HTTP request/response stubs for writing tests without a listening HTTP server - Swagger/OpenAPI spec validation

Classes

Class Description

TestSandbox

TestSandbox class provides a convenient way to get a reference to a sandbox folder in which you can perform operations for testing purposes.

Functions

Function Description

createClientForHandler(handler)

Create a SuperTest client connected to an HTTP server listening on an ephemeral port and calling handler to handle incoming requests.

createRestAppClient(app)

Create a SuperTest client for a running RestApplication instance. It is the responsibility of the caller to ensure that the app is running and to stop the application after all tests are done.

createStubInstance(constructor)

Creates a new object with the given functions as the prototype and stubs all implemented functions.

Note: The given constructor function is not invoked. See also the stub API.

This is a helper method replacing sinon.createStubInstance and working around the limitations of TypeScript and Sinon, where Sinon is not able to list private/protected members in the type definition of the stub instance and therefore the stub instance cannot be assigned to places expecting TType. See also - https://github.com/Microsoft/TypeScript/issues/13543 - https://github.com/DefinitelyTyped/DefinitelyTyped/issues/14811

createUnexpectedHttpErrorLogger(expectedStatusCode)

Creates a Logger that logs an Error if the HTTP status code is not expected

givenHttpServerConfig(customConfig)

Create an HTTP-server configuration that works well in test environments. - Ask the operating system to assign a free (ephemeral) port. - Use IPv4 localhost 127.0.0.1 to avoid known IPv6 issues in Docker-based environments like Travis-CI. - Provide default TLS key & cert when protocol is set to https.

httpGetAsync(urlString, agent)

Async wrapper for making HTTP GET requests

httpsGetAsync(urlString, agent)

Async wrapper for making HTTPS GET requests

inject(dispatchFunc, options)

skipIf(skip, verb, name, args)

Helper function for skipping tests when a certain condition is met.

skipOnTravis(verb, name, args)

Helper function for skipping tests on Travis CI.

stubExpressContext(requestOptions)

stubHandlerContext(requestOptions)

stubServerRequest(options)

stubServerResponse(request, onEnd)

toJSON(value)

toJSON(value)

toJSON(value)

toJSON(value)

toJSON(value)

toJSON(value)

toJSON(value)

toJSON(value)

toJSON(value)

toJSON(value)

JSON encoding does not preserve properties that are undefined As a result, deepEqual checks fail because the expected model value contains these undefined property values, while the actual result returned by REST API does not. Use this function to convert a model instance into a data object as returned by REST API

toJSON(value)

toJSON(value)

toJSON(value)

toJSON(value)

toJSON(value)

validateApiSpec(spec)

Interfaces

Interface Description

ExpressContextStub

HandlerContextStub

HostPort

An object that requires host and port properties

HttpOptions

HttpsOptions

RestApplicationLike

RestServerLike

TestSandboxOptions

Options for a test sandbox

Variables

Variable Description

expect

Type Aliases

Type Alias Description

Client

ObservedResponse

ShotCallback

ShotResponseCtor

StubbedInstanceWithSinonAccessor

TestDefinition

A function defining a new test case or a test suite, e.g. it or describe.