Skip to Content
GuidesTemplate Test Files

Template Test Files

Generate type tests using a data table.

This feature is available since TSTyche 4.0.

Usage

To mark a test file as a template, add the // @tstyche template comment directive at the top. When the directive is found, the default export of a file is interpreted as a type test text.

For example, the following:

// @tstyche template -- For documentation, see: https://tstyche.org/guides/template-test-files let = `import { expect, test } from "tstyche"; `; for (const of ["string", "number"]) { += `test("is ${} a string?", () => { expect<${}>().type.toBe<string>(); }); `; } export default ;

is interpreted as:

import { , } from "tstyche"; ("is string a string?", () => { <string>()..<string>(); }); ("is number a string?", () => { <number>()..<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 .

Last updated on