-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add examples and use cases (#619)
- Loading branch information
1 parent
cd30ff0
commit 6a204cd
Showing
5 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
--- | ||
tags: [examples, promise, tutorial, parameterized, parametrized] | ||
sidebar_position: 0.5 | ||
--- | ||
|
||
import Tabs from '@theme/Tabs'; | ||
import TabItem from '@theme/TabItem'; | ||
import MidLevel from '@site/static/img/mid-level.svg'; | ||
|
||
# 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: | ||
|
||
```ts | ||
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: | ||
|
||
```ts | ||
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`. | ||
::: | ||
|
||
<hr /> | ||
|
||
:::info | ||
These examples were based on [this comment](https://github.com/wellwelwel/poku/issues/566#issuecomment-2241496155). | ||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters