how do I use an array of test data pairs with Deno Test?

Is there a way for me to run an array of test data pairs? If I do it like this, it will stop after the first failure.
const data: [string, STRINGorUNDEFINED][] = [
["#withpound", "withpound"],
["nopound", "nopound"],
[" leading space", undefined],
["", undefined],
];

Deno.test("truth test", () => {
data.forEach((pair) => {
const input = pair[0];
const expected = pair[1];
const actual: RETURNTYPE = parseTagLine(input);
assertEquals(actual, expected);
});
});
const data: [string, STRINGorUNDEFINED][] = [
["#withpound", "withpound"],
["nopound", "nopound"],
[" leading space", undefined],
["", undefined],
];

Deno.test("truth test", () => {
data.forEach((pair) => {
const input = pair[0];
const expected = pair[1];
const actual: RETURNTYPE = parseTagLine(input);
assertEquals(actual, expected);
});
});
Is there something better than simple inversion?
data.forEach((pair) => {
Deno.test("truth test", () => {
const input = pair[0];
const expected = pair[1];
const actual: RETURNTYPE = parseTagLine(input);
assertEquals(actual, expected);
});
});
data.forEach((pair) => {
Deno.test("truth test", () => {
const input = pair[0];
const expected = pair[1];
const actual: RETURNTYPE = parseTagLine(input);
assertEquals(actual, expected);
});
});
Is there a way for me to run an array of test data pairs? If I do it like this, it will stop after the first failure.
const data: [string, STRINGorUNDEFINED][] = [
["#withpound", "withpound"],
["nopound", "nopound"],
[" leading space", undefined],
["", undefined],
];

Deno.test("truth test", () => {
data.forEach((pair) => {
const input = pair[0];
const expected = pair[1];
const actual: RETURNTYPE = parseTagLine(input);
assertEquals(actual, expected);
});
});
const data: [string, STRINGorUNDEFINED][] = [
["#withpound", "withpound"],
["nopound", "nopound"],
[" leading space", undefined],
["", undefined],
];

Deno.test("truth test", () => {
data.forEach((pair) => {
const input = pair[0];
const expected = pair[1];
const actual: RETURNTYPE = parseTagLine(input);
assertEquals(actual, expected);
});
});
Is there something better than simple inversion?
data.forEach((pair) => {
Deno.test("truth test", () => {
const input = pair[0];
const expected = pair[1];
const actual: RETURNTYPE = parseTagLine(input);
assertEquals(actual, expected);
});
});
data.forEach((pair) => {
Deno.test("truth test", () => {
const input = pair[0];
const expected = pair[1];
const actual: RETURNTYPE = parseTagLine(input);
assertEquals(actual, expected);
});
});
3 Replies
KyleJune
KyleJune2y ago
Call Deno.test inside your forEach. If you want each test name to be unique to the case you could generate test name based on the pair. What would you consider better than that or what is it that you are looking for? In a forEach or a for loop seems like a pretty clean solution.
KyleJune
KyleJune2y ago
If what you want is for your data based test cases to be grouped together, you could use the test step api, generating steps instead of top level tests. You can do similar nesting/grouping with bdd. This manual page has examples of test steps and using the std/testing/bdd module. https://deno.land/manual@v1.26.1/testing/behavior_driven_development
Deno
Behavior-Driven Development | Manual | Deno
With the bdd.ts module you can write your tests in a familiar format for grouping tests and adding setup/teardown hooks used by other JavaScript t
dan.the.discloser
thank you. In pytest there is "parametrize" as a standard way to do this. Was wondering if that was a standard stylied manner here but I can do it exactly as you describe thank you.