Reference
Testing API

Testing API

TSTyche ships testing helpers and run mode flags to organize, select and filter tests to run.

Run Mode Flags

To select groups or tests to run, append the run mode flags to any describe() or test() function. The flags are inherited from the parent.

import { describe, expect, test } from "tstyche";
 
describe.only("string", () => {
  test("is a string?", () => {
    expect<string>().type.toBeString();
  });
 
  test.skip("does not run", () => {
    expect<string>().type.not.toBeString();
  });
});
 
describe("number", () => {
  test("does not run", () => {
    expect<number>().type.toBeNumber();
  });
 
  test.only("is not a number?", () => {
    expect<string>().type.not.toBeNumber();
  });
});
 
test.todo("to be implemented");

.only

Marks a group or a test as focused. When there are focused groups, tests or assertions in a file, only those will run.

.skip

Marks a group or a test as skipped. The test runner will ignore anything skipped and will suppress any type errors raised within the bodies of the callback functions.

.todo

Marks a group or a test as yet to be implemented. It does everything that the .skip flag does and also makes the callback function optional.

You can use the .only and .skip flags with the expect() function as well.

Testing Helpers

TSTyche does not require to use the helper functions. For instance, instead of callbacks you can use block statements (opens in a new tab) to scope variables or code comments to name the tests:

import { expect } from "tstyche";
 
// is a string?
{
  expect<string>().type.toBeString();
}

This also works, but the helpers have more to offer. First, they provide the run mode flags to select tests to run. Second, you can filter tests by name from the command line through the --only and --skip options.

Keep in mind that the runner does not execute the code, therefor the name argument must always be a string literal.

describe(name, callback)

  • name: string The name of the group.
  • callback: () => void | Promise<void> The function to create a scope for a group of tests.

Defines a group of tests. The describe() groups can be nested within each other.

import { describe, expect, test } from "tstyche";
 
describe("is a group?", () => {
  test("is a string?", () => {
    expect<string>().type.toBeString();
  });
 
  test("is a number?", () => {
    expect<number>().type.toBeNumber();
  });
});

it(name, callback)

An alias for test(). For example, you can use it to write tests in the BDD style (opens in a new tab):

import { describe, expect, it } from "tstyche";
 
describe("number", () => {
  it("should be of type 'number'", () => {
    expect<number>().type.toBeNumber();
  });
});

test(name, callback)

  • name: string The name of the test.
  • callback: () => void | Promise<void> The function with a code snippet and assertions.

Defines a single test.

import { expect, test } from "tstyche";
 
test("is a string?", () => {
  expect<string>().type.toBeString();
});