Guide
Template Test Files

Template Test Files

The template test files allow generating tests using a template and data table.

This feature is available since TSTyche 4.0.

Usage

To mark a test file as a template, add the // @tstyche-template directive as its first line. When the directive is found, the default export of a file is interpreted as a type test text.

For example, the following:

// @tstyche-template
 
let testText = `import { expect, test } from "tstyche";
`;
 
for (const source of ["string", "number"]) {
  testText += `test("is ${source} a string?", () => {
  expect<${source}>().type.toBe<string>();
});
`;
}
 
export default testText;

is interpreted as:

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

This feature requires Node.js 22.6 or later.

The type test text is loaded using the import() expression and replaced programmatically keeping the same file name. Therefore, this must be a TypeScript file and it must be executed at the same time. Fortunately, running TypeScript code with Node.js is now possible (opens in a new tab).

Last updated on