Testing TypeScript Code
Table Driven Cases
A test table lets one loop run the same check against several inputs.
Table Driven Cases
table_cases.ts
type Case = {
input: string;
expected: string;
};
function normalizeSlug(text: string): string {
return text.trim().toLowerCase().replaceAll(" ", "-");
}
const includeThird: boolean = ;
const cases: Case[] = [
{ input: "Hello World", expected: "hello-world" },
{ input: " Type Script ", expected: "type-script" },
];
if (includeThird) {
cases.push({ input: "Replay Data", expected: "replay-data" });
}
const passed: number = cases.filter((testCase) => normalizeSlug(testCase.input) === testCase.expected).length;
console.log(`${passed}/${cases.length}`);
type Case = {
input: string;
expected: string;
};
function normalizeSlug(text: string): string {
return text.trim().toLowerCase().replaceAll(" ", "-");
}
const includeThird: boolean = ;
const cases: Case[] = [
{ input: "Hello World", expected: "hello-world" },
{ input: " Type Script ", expected: "type-script" },
];
if (includeThird) {
cases.push({ input: "Replay Data", expected: "replay-data" });
}
const passed: number = cases.filter((testCase) => normalizeSlug(testCase.input) === testCase.expected).length;
console.log(`${passed}/${cases.length}`);
test table
A table-driven test stores inputs and expected outputs together so the same test logic stays compact.