Parameterized Tests
Parameterized tests allow you to run the same test logic with different inputs and expected results. This helps to test across various scenarios without writing repetitive tests.
For example:
import { assert, test } from 'poku';
const testCases = [
{
expected: true,
input: { name: 'Alice', role: 'admin' },
testCase: 'is admin',
},
{
expected: false,
input: { name: 'Bob', role: 'user' },
testCase: 'is not admin',
},
];
const isAdmin = (user) => user.role === 'admin';
for (const { expected, input, testCase } of testCases) {
test(testCase, () => {
const actual = isAdmin(input);
assert.strictEqual(actual, expected);
});
}
Using promisesβ
Handling asynchronous operations sequentially within parameterized tests using promises:
import { assert, test } from 'poku';
const testCases = [
{
expected: true,
input: { name: 'Alice', role: 'admin' },
testCase: 'is admin',
},
{
expected: false,
input: { name: 'Bob', role: 'user' },
testCase: 'is not admin',
},
];
const isAdmin = (user) => Promise.resolve(user.role === 'admin');
for (const { expected, input, testCase } of testCases) {
await test(testCase, async () => {
const actual = await isAdmin(input);
assert.strictEqual(actual, expected);
});
}
tip
To run asynchronous operations in parallel, simply remove await
from test
or it
.
info
These examples were based on this comment.