0.2.4 • Published 14 days ago

@microcks/microcks-testcontainers v0.2.4

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
14 days ago

Microcks Testcontainers Node

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

GitHub Workflow Status Version License Project Chat

Build Status

Latest released version is 0.2.4.

Current development version is 0.2.5.

How to use it?

Include it into your project dependencies

npm install @microcks/microcks-testcontainers

Startup the container

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.2 and above use version 1.8.1 of Microcks container images.

import { MicrocksContainer } from "@microcks/microcks-testcontainers";

const container = await new MicrocksContainer().start();

Import content in Microcks

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();

Using mock endpoints for your dependencies

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.

Launching new contract-tests

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.

Using authentication Secrets

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
}

Advanced features with MicrocksContainersEnsemble

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 Microks services. MicrocksContainersEnsemble allow you to implement Different levels of API contract testing in the Inner Loop with Testcontainers!

A MicrocksContainersEnsemble conforms to Testcontaierns lifecycle methods and presents roughly the same interface as a MicrocksContainer. To 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.

Postman contract-testing

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 support

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();
Using mock endpoints for your dependencies

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");
Launching new contract-tests

Using contract-testing techniques on Asynchronous endpoints may require a different style of interacting with the Microcks container. For example, you may need to: 1. Start the test making Microcks listen to the target async endpoint, 2. Activate your System Under Tests so that it produces an event, 3. 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);
0.2.4

14 days ago

0.2.3

1 month ago

0.2.2

4 months ago

0.2.1

6 months ago

0.2.0

7 months ago

0.1.2

7 months ago

0.1.1

7 months ago

0.1.0

8 months ago