skipcheck

TM1 Testcontainers · Docs · Writing tests

RequiresTm1

import { RequiresTm1 } from "@skipcheck/tm1-testcontainers";

Used in the walkthrough Test your code against a real TM1.

Runs a test class or method against a real TM1 server, and skips it when there isn't one.

Tests are skipped, not failed, when Docker isn't available or no Tm1Source is configured, so a build without IBM software (such as GitHub CI) stays green. A source that is configured but wrong, such as a folder with no TM1 in it, fails instead.

Declare a Tm1ServerContainer parameter on a test method, constructor or lifecycle method to get the shared container, started on first use.

Tests share one server, so each test should create its own uniquely named objects and delete them afterwards. A test class that sets up a client once and cleans up after each test:

describe("the Region dimension", async () => {
  // Setup for the whole file needs the server too, so skip it as the tests are skipped
  const skipped = (await RequiresTm1.skipReason()) !== undefined;
  let tm1: Tm1Client;

  // One client per file, on the shared server
  beforeAll(async () => {
    if (skipped) return;
    const server = await Tm1ServerContainer.shared();
    tm1 = Tm1Client.builder().baseUrl(server.getRestUrl()).basicAuth(server.getUsername(), server.getPassword()).build();
  });

  afterAll(async () => {
    await tm1?.close();
  });

  // The server is shared, so use a unique name and delete it afterwards
  const region = "Region-" + randomUUID();

  afterEach(async () => {
    await tm1?.dimensions.delete(region).catch(() => undefined);
  });

  requiresTm1("creates the regions", async () => {
    const elements = ["North", "South"].map((name) => ElementCreate.builder().name(name).type(ElementType.Numeric).build());
    const hierarchy = HierarchyCreate.builder().name(region).elements(elements).build();
    const created = await tm1.dimensions.create(DimensionCreate.builder().name(region).hierarchies([hierarchy]).build());
    expect(created.Name).toBe(region);
  });
});

A test that only applies to some TM1 versions checks the server's and skips:

requiresTm1("needs a recent server", async ({ tm1, skip }) => {
  // Skips, rather than fails, on servers older than the feature
  const introducedIn = Tm1Version.parse("11.8.03000");
  if (!tm1.getTm1Version().isAtLeast(introducedIn)) skip(`needs TM1 ${introducedIn.toString()} or newer`);
  // ...
});

Members

NO_SOURCE_MESSAGE staticWhy a test is skipped without a source.
NO_DOCKER_MESSAGE staticWhy a test is skipped without Docker.
skipReason staticWhy a TM1 test would be skipped, or undefined to run it (a wrong source runs it, to fail).

NO_SOURCE_MESSAGE

static
NO_SOURCE_MESSAGE

Why a test is skipped without a source.

NO_DOCKER_MESSAGE

static
NO_DOCKER_MESSAGE

Why a test is skipped without Docker.

skipReason

static
static skipReason(): Promise<string | undefined>

Why a TM1 test would be skipped, or undefined to run it (a wrong source runs it, to fail).