Node/TypeScript library for Testcontainers that enables embedding Microcks into your unit tests with lightweight, throwaway instance thanks to containers
Want to see this extension in action? Check out our sample application. 🚀
Latest released version is 0.2.5
.
Current development version is 0.2.6
.
- Documentation
- Microcks Community and community meeting
- Join us on Discord, on GitHub Discussions or CNCF Slack #microcks channel
To get involved with our community, please make sure you are familiar with the project's Code of Conduct.
npm install @microcks/microcks-testcontainers
You just have to specify the container image you'd like to use. This library requires a Microcks uber
distribution (with no MongoDB dependency).
Version 0.2.5
and above use version 1.10.0
of Microcks container images.
import { MicrocksContainer } from "@microcks/microcks-testcontainers";
const container = await new MicrocksContainer().start();
To use Microcks mocks or contract-testing features, you first need to import OpenAPI, Postman Collection, GraphQL or gRPC artifacts. Artifacts can be imported as main/Primary ones or as secondary ones. See Multi-artifacts support for details.
You can do it before starting the container using arrays of paths:
import * as path from "path";
const resourcesDir = path.resolve(__dirname, "..", "test-resources");
const container = await new MicrocksContainer()
.withMainArtifacts([path.resolve(resourcesDir, "apipastries-openapi.yaml")])
.withSecondaryArtifacts([path.resolve(resourcesDir, "apipastries-postman-collection.json")])
.start();
or once the container is started :
import * as path from "path";
const resourcesDir = path.resolve(__dirname, "..", "test-resources");
await container.importAsMainArtifact(path.resolve(resourcesDir, "apipastries-openapi.yaml"));
await container.importAsSecondaryArtifact(path.resolve(resourcesDir, "apipastries-postman-collection.json"));
Please refer to our MicrocksContainerTest for comprehensive example on how to use it.
Starting with version 0.2.4
you can also import full
repository snapshots at once:
import * as path from "path";
const resourcesDir = path.resolve(__dirname, "..", "test-resources");
const container = await new MicrocksContainer()
.withSnapshots([path.resolve(resourcesDir, "microcks-repository.json")])
.start();
During your test setup, you'd probably need to retrieve mock endpoints provided by Microcks containers to setup your base API url calls. You can do it like this:
// Get base Url for API Pastries / 0.0.1
var pastriesUrl = container.getRestMockEndpoint("API Pastries", "0.0.1");
The container provides methods for different supported API styles/protocols (Soap, GraphQL, gRPC,...).
The container also provides getHttpEndpoint()
for raw access to those API endpoints.
If you want to ensure that your application under test is conformant to an OpenAPI contract (or many contracts), you can launch a Microcks contract/conformance test using the local server port you're actually running:
import { MicrocksContainer, TestRequest, TestRunnerType } from "@microcks/microcks-testcontainers";
var testRequest: TestRequest = {
serviceId: "API Pastries:0.0.1",
runnerType: TestRunnerType.OPEN_API_SCHEMA,
testEndpoint: "http://bad-impl:3001",
timeout: 2000
}
var testResult = await container.testEndpoint(testRequest);
expect(testResult.success).toBe(false);
expect(testResult.testedEndpoint).toBe("http://bad-impl:3001");
expect(testResult.testCaseResults.length).toBe(3);
expect(testResult.testCaseResults[0].testStepResults[0].message).toContain("object has missing required properties");
The TestResult
gives you access to all details regarding success of failure on different test cases.
A comprehensive NestJS demo application illustrating both usages is available here: nest-order-service.
It's a common need to authenticate to external systems like Http/Git repositories or external brokers. For that, the MicrocksContainer
provides the withSecret()
method to register authentication secrets at startup:
microcks.withSecret({
name: 'localstack secret',
username: 'test',
password: 'test'
})
.start();
You may reuse this secret using its name later on during a test like this:
const testRequest: TestRequest = {
serviceId: "Pastry orders API:0.1.0",
runnerType: TestRunnerType.ASYNC_API_SCHEMA,
testEndpoint: "sqs://us-east-1/pastry-orders?overrideUrl=http://localstack:4566",
secretName: "localstack secret",
timeout: 5000
}
The MicrocksContainer
referenced above supports essential features of Microcks provided by the main Microcks container. The list of supported features is the following:
- Mocking of REST APIs using different kinds of artifacts,
- Contract-testing of REST APIs using
OPEN_API_SCHEMA
runner/strategy, - Mocking and contract-testing of SOAP WebServices,
- Mocking and contract-testing of GraphQL APIs,
- Mocking and contract-testing of gRPC APIs.
To support features like POSTMAN
contract-testing, we introduced MicrocksContainersEnsemble that allows managing additional Microcks services. MicrocksContainersEnsemble allow you to implement Different levels of API contract testing in the Inner Loop with Testcontainers!
A MicrocksContainersEnsemble
conforms to Testcontainers lifecycle methods and presents roughly the same interface as a MicrocksContainer
. You can create and build an ensemble that way after having initialized a Network
:
import { Network } from "testcontainers";
import { MicrocksContainer } from "@microcks/microcks-testcontainers";
const network = await new Network().start();
const ensemble = await new MicrocksContainersEnsemble(network)
.withMainArtifacts([path.resolve(resourcesDir, "apipastries-openapi.yaml")])
.withSecondaryArtifacts([path.resolve(resourcesDir, "apipastries-postman-collection.json")])
.start();
A MicrocksContainer
is wrapped by an ensemble and is still available to import artifacts and execute test methods. You have to access it using:
const microcks = ensemble.getMicrocksContainer();
await microcks.importAsMainArtifact(...);
(await microcks.logs())
.on("data", line => console.log(line))
.on("err", line => console.error(line))
.on("end", () => console.log("Stream closed"));
Please refer to our MicrocksContainersEnsembleTest for comprehensive example on how to use it.
On this ensemble
you may want to enable additional features such as Postman contract-testing:
ensemble.withPostman();
ensemble.start();
You can execute a `POSTMAN`` test using an ensemble that way:
var testRequest = {
serviceId: "API Pastries:0.0.1",
runnerType: TestRunnerType.POSTMAN,
testEndpoint: "http://good-impl:3003",
timeout: 3000
}
var testResult = await ensemble.getMicrocksContainer().testEndpoint(testRequest);
expect(testResult.success).toBe(true);
expect(testResult.testedEndpoint).toBe("http://good-impl:3003");
expect(testResult.testCaseResults.length).toBe(3);
expect(testResult.testCaseResults[0].testStepResults[0].message).toBeUndefined();
Asynchronous API feature need to be explicitly enabled as well. In the case you want to use it for mocking purposes, you'll have to specify additional connection details to the broker of your choice. See an example below with connection to a Kafka broker:
ensemble.withAsyncFeature()
.withKafkaConnection({
bootstrapServers: "kafka:9092"
});
ensemble.start();
Once started, the ensemble.getAsyncMinionContainer()
provides methods for retrieving mock endpoint names for the different
supported protocols (WebSocket, Kafka, SQS and SNS).
const kafkaTopic = ensemble.getAsyncMinionContainer()
.getKafkaMockTopic("Pastry orders API", "0.1.0", "SUBSCRIBE pastry/orders");
Using contract-testing techniques on Asynchronous endpoints may require a different style of interacting with the Microcks container. For example, you may need to:
- Start the test making Microcks listen to the target async endpoint,
- Activate your System Under Tests so that it produces an event,
- Finalize the Microcks tests and actually ensure you received one or many well-formed events.
As the MicrocksContainer
provides the testEndpoint(request: TestRequest): Promise<TestResult>
method, this is actually easy like:
// Start the test, making Microcks listen the endpoint provided in testRequest
let testResultPromise: Promise<TestResult> = ensemble.getMicrocksContainer().testEndpoint(testRequest);
// Here below: activate your app to make it produce events on this endpoint.
// myapp.invokeBusinessMethodThatTriggerEvents();
// Now retrieve the final test result and assert.
let testResult = await testResultPromise;
expect(testResult.success).toBe(true);