summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYasser A.Idrissi <getspookydev@gmail.com>2021-03-15 15:47:14 +0100
committerGitHub <noreply@github.com>2021-03-15 10:47:14 -0400
commit0ae079fe50c674824e86a1a0aeb4d2dbd5721dbf (patch)
treed952f49e7a91b26b7a0fc17a3e8459cc58f6d6fb
parentb2a1ad0611e484b57420925f51393e57c679815c (diff)
docs(testing): Add custom test example (#9791)
-rw-r--r--docs/testing/assertions.md30
1 files changed, 30 insertions, 0 deletions
diff --git a/docs/testing/assertions.md b/docs/testing/assertions.md
index 7d7bc69b3..5fca77832 100644
--- a/docs/testing/assertions.md
+++ b/docs/testing/assertions.md
@@ -231,3 +231,33 @@ Deno.test("Test Assert Equal Fail Custom Message", () => {
assertEquals(1, 2, "Values Don't Match!");
});
```
+
+### Custom Tests
+
+While Deno comes with powerful
+[assertions modules](https://deno.land/std@$STD_VERSION/testing/asserts.ts) but
+there is always something specific to the project you can add. Creating
+`custom assertion function` can improve readability and reduce the amount of
+code.
+
+```js
+function assertPowerOf(actual: number, expected: number, msg?: string): void {
+ let received = actual;
+ while (received % expected === 0) received = received / expected;
+ if (received !== 1) {
+ if (!msg) {
+ msg = `actual: "${actual}" expected to be a power of : "${expected}"`;
+ }
+ throw new AssertionError(msg);
+ }
+}
+```
+
+Use this matcher in your code like this:
+
+```js
+Deno.test("Test Assert PowerOf", () => {
+ assertPowerOf(8, 2);
+ assertPowerOf(11, 4);
+});
+```