@marblejs/testing is a tool agnostic module for testing Marble.js apps.
Installation
To get started simply install testing module in your project as a dev dependency. You can use any testing framework that you like, but for example purposes will show and stick to Jest testing environment.
$yarnadd--dev@marblejs/testing
HttpTestBed
In order to test Marble.js HTTP APIs you have to create a TestBed instance. It is a primary API with a role to configure and initialize environment for testing, overriding and injecting bound dependencies. For HTTP protocol specific use cases we will use createHttpTestBed factory function. In case of HttpTestBed type you have to provide a required HTTP listener instance and optionally default headers that will be applied for each tested request.
You can inject any bound dependencies to the TestBed constructor and ask for them eg. via useContext hook function.
Usually testing HTTP APIs is very repetitive - you have to create a TestBed instance, provide all basic dependencies, send a request, assert the response and finish (close) hanging test connection. In order to make things simple and more DRY, @marblejs/testing defines a useful function for defining a test setup that you can refer to in any *.spec file, which can save a lot of unnecessary boilerplate for defining test environment. All you have to do is to pass previously defined TestBed instance to createTestBedSetup function and define a default set of dependencies that later on can be overridden in test cases. Additionally you can define a set of cleanups that will be triggered on close/finish. We will go back to this topic later.
The main role of the TestBed, besides constructing and sending test requests, is environment setup and context preparation. You can override any binding you want and ask for bound instances in order to do some pre-test preparations.
Marble.js defines similar pipeable API as for constructing HTTP routes. It defines a set of useful functions for building and sending a request in a functional way.
Sometimes there is a need to do some cleanup stuff after each test. Of course, you can inject and close all hanging connections manually for each dependency that requires it, but as you probably know... it is not a best idea. Marble.js cannot automagically close all established connections that bound dependencies created, but you can instruct TestBedSetup by defining a set of cleanups to trigger a desired job after finish.
Let's assume that we have some custom dependency that their interface defines an async close method.