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](/doc/en/lb4/apidocs.testlab.testsandbox.html) 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)](/doc/en/lb4/apidocs.testlab.createclientforhandler.html) Create a SuperTest client connected to an HTTP server listening on an ephemeral port and calling `handler` to handle incoming requests.
[createRestAppClient(app)](/doc/en/lb4/apidocs.testlab.createrestappclient.html) 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)](/doc/en/lb4/apidocs.testlab.createstubinstance.html) 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)](/doc/en/lb4/apidocs.testlab.createunexpectedhttperrorlogger.html) Creates a Logger that logs an Error if the HTTP status code is not expected
[givenHttpServerConfig(customConfig)](/doc/en/lb4/apidocs.testlab.givenhttpserverconfig.html) 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)](/doc/en/lb4/apidocs.testlab.httpgetasync.html) Async wrapper for making HTTP GET requests
[httpsGetAsync(urlString, agent)](/doc/en/lb4/apidocs.testlab.httpsgetasync.html) Async wrapper for making HTTPS GET requests
[inject(dispatchFunc, options)](/doc/en/lb4/apidocs.testlab.inject.html)
[skipIf(skip, verb, name, args)](/doc/en/lb4/apidocs.testlab.skipif.html) Helper function for skipping tests when a certain condition is met.
[skipOnTravis(verb, name, args)](/doc/en/lb4/apidocs.testlab.skipontravis.html) Helper function for skipping tests on Travis CI.
[stubExpressContext(requestOptions)](/doc/en/lb4/apidocs.testlab.stubexpresscontext.html)
[stubHandlerContext(requestOptions)](/doc/en/lb4/apidocs.testlab.stubhandlercontext.html)
[stubServerRequest(options)](/doc/en/lb4/apidocs.testlab.stubserverrequest.html)
[stubServerResponse(request, onEnd)](/doc/en/lb4/apidocs.testlab.stubserverresponse.html)
[toJSON(value)](/doc/en/lb4/apidocs.testlab.tojson.html)
[toJSON(value)](/doc/en/lb4/apidocs.testlab.tojson_9.html)
[toJSON(value)](/doc/en/lb4/apidocs.testlab.tojson_10.html)
[toJSON(value)](/doc/en/lb4/apidocs.testlab.tojson_11.html)
[toJSON(value)](/doc/en/lb4/apidocs.testlab.tojson_12.html)
[toJSON(value)](/doc/en/lb4/apidocs.testlab.tojson_13.html)
[toJSON(value)](/doc/en/lb4/apidocs.testlab.tojson_14.html)
[toJSON(value)](/doc/en/lb4/apidocs.testlab.tojson_1.html)
[toJSON(value)](/doc/en/lb4/apidocs.testlab.tojson_2.html)
[toJSON(value)](/doc/en/lb4/apidocs.testlab.tojson_3.html) 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)](/doc/en/lb4/apidocs.testlab.tojson_4.html)
[toJSON(value)](/doc/en/lb4/apidocs.testlab.tojson_5.html)
[toJSON(value)](/doc/en/lb4/apidocs.testlab.tojson_6.html)
[toJSON(value)](/doc/en/lb4/apidocs.testlab.tojson_7.html)
[toJSON(value)](/doc/en/lb4/apidocs.testlab.tojson_8.html)
[validateApiSpec(spec)](/doc/en/lb4/apidocs.testlab.validateapispec.html)

Interfaces

Interface Description
[ExpressContextStub](/doc/en/lb4/apidocs.testlab.expresscontextstub.html)
[HandlerContextStub](/doc/en/lb4/apidocs.testlab.handlercontextstub.html)
[HostPort](/doc/en/lb4/apidocs.testlab.hostport.html) An object that requires host and port properties
[HttpOptions](/doc/en/lb4/apidocs.testlab.httpoptions.html)
[HttpsOptions](/doc/en/lb4/apidocs.testlab.httpsoptions.html)
[RestApplicationLike](/doc/en/lb4/apidocs.testlab.restapplicationlike.html)
[RestServerLike](/doc/en/lb4/apidocs.testlab.restserverlike.html)
[TestSandboxOptions](/doc/en/lb4/apidocs.testlab.testsandboxoptions.html) Options for a test sandbox

Variables

Variable Description
[expect](/doc/en/lb4/apidocs.testlab.expect.html)

Type Aliases

Type Alias Description
[Client](/doc/en/lb4/apidocs.testlab.client.html)
[ObservedResponse](/doc/en/lb4/apidocs.testlab.observedresponse.html)
[ShotCallback](/doc/en/lb4/apidocs.testlab.shotcallback.html)
[ShotResponseCtor](/doc/en/lb4/apidocs.testlab.shotresponsector.html)
[StubbedInstanceWithSinonAccessor](/doc/en/lb4/apidocs.testlab.stubbedinstancewithsinonaccessor.html)
[TestDefinition](/doc/en/lb4/apidocs.testlab.testdefinition.html) A function defining a new test case or a test suite, e.g. `it` or `describe`.