|
7 | 7 |
|
8 | 8 | Merlin is a [Jest](https://jestjs.io/en/)-inspired testing framework for deno. |
9 | 9 |
|
10 | | -### use |
| 10 | +## Using Matchers |
11 | 11 |
|
12 | | -example `merlin.test.ts` |
| 12 | +### Common Matchers |
13 | 13 |
|
14 | | -```typescript |
| 14 | +- `testEqual(label: string, config)` |
| 15 | +- `testNotEqual(label: string, config)` |
| 16 | +- `evalEquals(testEqual[])` |
| 17 | +- `stringContains(label: string, config)` |
| 18 | +- `arrayContains(label: string, config)` |
| 19 | +- `beNull(label: string, config)` |
| 20 | +- `beFalsy(label: string, config)` |
| 21 | +- `beTruthy(label: string, config)` |
| 22 | + |
| 23 | +### All Matchers |
| 24 | + |
| 25 | +- `testEqual(label: string, config)`_Compare two values and throws an error if the expect and toBe are not equal._ |
| 26 | + |
| 27 | +- `testNotEqual(label: string, config)`_Compare two values and throws an error if the expect and notBe are equal._ |
| 28 | + |
| 29 | +- `evalEquals(testEqual[])` _evaluate multiple equality tests in an array. If the data is not the same it throws an error._ |
| 30 | + |
| 31 | +- `fetchEqual(label: string, config)` _evaluate if two values are equal. If the request data is not the same as expected, it throws an error._ |
| 32 | + |
| 33 | +- `arrayContains(label: string, config)`_evaluates that the array contains an especific data. if the array does not contain the data it throws an error._ |
| 34 | + |
| 35 | +- `stringContains(label: string, config)`_evaluates if a string contains an especific word. if the string does not contain the word it throws an error._ |
| 36 | + |
| 37 | +- `beNull(label: string, config)` _evaluates if a data is null._ |
| 38 | + |
| 39 | +- `beFalsy(label: string, config)`_evaluates if a data is a falsy value._ |
| 40 | + |
| 41 | +- `beTruthy(label: string, config)`_evaluates if a data is a truthy value._ |
| 42 | + |
| 43 | +- `isBigInt(label: string, config)`_evaluates if a data is a bigInt value type._ |
| 44 | + |
| 45 | +- `isZero(label: string, config)`_evaluates if a data is a Zero_ |
15 | 46 |
|
| 47 | +- `isNaN(label: string, config)`_evaluates if a data is NaN value._ |
| 48 | + |
| 49 | +- `sameLength(label: string, config)`_evaluates if data has a especific length_ |
| 50 | + |
| 51 | +- `testRegExp(label: string, config)`_evaluates if a regular expression match_ |
| 52 | + |
| 53 | +- `isFunction(label: string, config)` _evaluates if a data is a function_ |
| 54 | + |
| 55 | +- `isSymbol(label: string, config)`_evaluates if a data is a symbol_ |
| 56 | + |
| 57 | +- `isUndefined(label: string, config)`_evaluates if a data is undefined_ |
| 58 | + |
| 59 | +### Basic Use |
| 60 | + |
| 61 | +simple assertions. |
| 62 | + |
| 63 | +`example.test.ts` |
| 64 | + |
| 65 | +```typescript |
16 | 66 | import { Merlin } from "https://deno.land/x/merlin/mod.ts"; |
17 | 67 |
|
18 | 68 | const test = new Merlin(); |
19 | 69 |
|
20 | | -test.test_equal("2 + 2", { |
21 | | - message: "error adding 2 + 2", |
22 | | - expect: () => { |
| 70 | +test.testEqual("two plus two is four", { |
| 71 | + expect() { |
23 | 72 | return 2 + 2; |
24 | 73 | }, |
25 | | - toBe: () => { |
| 74 | + toBe() { |
26 | 75 | return 4; |
27 | 76 | }, |
28 | | - ignore: Deno.build.os === "linux", |
29 | 77 | }); |
| 78 | +``` |
| 79 | + |
| 80 | +run this test in deno |
| 81 | + |
| 82 | +```sh |
| 83 | +$ deno test |
| 84 | +``` |
| 85 | + |
| 86 | +you should see this output on the console. |
| 87 | + |
| 88 | +```sh |
| 89 | +running 1 tests |
| 90 | +test two plus two is four ... ok (17ms) |
| 91 | + |
| 92 | +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (18ms) |
| 93 | +``` |
| 94 | + |
| 95 | +### Parameters |
| 96 | + |
| 97 | +all assertions have parameters that they can receive, these parameters can change the behavior of the tests. |
| 98 | + |
| 99 | +- `label` add a description to the test. |
| 100 | +- `expect()` this function returns the data and then tests with its matchmaker. |
| 101 | +- `toBe()` this function returns the data that we hope is correct. |
| 102 | +- `notBe()` this function returns the data that we hope it is incorrect. |
| 103 | +- `value()` returns the data expected to be of that type. |
| 104 | +- `ignore (optional)` receives a boolean to ignore the test in case the value is true. |
| 105 | +- `strict (optional)` receives a boolean, it does a strict comparison of the `expect()` and `toBe()` values. |
| 106 | +- `message (optional)` receives a string with the message to display in case the test fails. |
| 107 | +- `Ops (optional)` receives a boolean, closes all the operations that never end, for example `Deno.open("file.txt")`. by default is `true`. |
| 108 | +- `Resources (optional)` receives a boolean, terminates all asynchronous processes that interact with the system. by default is `true`. |
| 109 | +- `only (optional)` receives a boolean, only tests that have `only in true` will be executed, the rest will not run. |
| 110 | + |
| 111 | +## about resources and ops sanitizers |
| 112 | + |
| 113 | +Certain actions in Deno create resources in the resource table . These resources should be closed after you are done using them. |
| 114 | + |
| 115 | +For each test definition, the test runner checks that all resources created in this test have been closed. This is to prevent resource 'leaks'. This is enabled by default for all tests, but can be disabled by setting the sanitizeResources boolean to false in the test definition. |
| 116 | + |
| 117 | +The same is true for async operation like interacting with the filesystem. The test runner checks that each operation you start in the test is completed before the end of the test. This is enabled by default for all tests, but can be disabled by setting the sanitizeOps boolean to false in the test definition. |
| 118 | + |
| 119 | +```typescript |
| 120 | +async function writeSomething(): Promise<string> { |
| 121 | + const decoder = new TextDecoder("utf-8"); |
| 122 | + Deno.createSync("./texts.txt"); |
| 123 | + const Package = await Deno.readFileSync("./text.txt"); |
| 124 | + await Deno.writeTextFile("./text.txt", "test"); |
| 125 | + return decoder.decode(Package); |
| 126 | +} |
| 127 | + |
| 128 | +test.testEqual("Leak resources test", { |
| 129 | + expect:() => "test", |
| 130 | + toBe:() =>writeSomething(), |
| 131 | + only: true, |
| 132 | + Ops: false, |
| 133 | + Resources: false |
| 134 | +}); |
| 135 | +``` |
| 136 | +```sh |
| 137 | +deno test |
| 138 | + |
| 139 | +test Leak resources test ... ok (5ms) |
| 140 | + |
| 141 | +test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out |
| 142 | +``` |
| 143 | +### Multiple tests |
| 144 | + |
| 145 | +`example.test.ts` |
30 | 146 |
|
| 147 | +```typescript |
| 148 | +test.evalEquals([ |
| 149 | + { |
| 150 | + label: "object assignment", |
| 151 | + expect() { |
| 152 | + const data: any = { one: 1 }; |
| 153 | + data["two"] = 2; |
| 154 | + |
| 155 | + return data; |
| 156 | + }, |
| 157 | + toBe() { |
| 158 | + return { one: 1, two: 2 }; |
| 159 | + }, |
| 160 | + }, |
| 161 | + { |
| 162 | + label: "two plus two is four", |
| 163 | + expect() { |
| 164 | + return 2 + 2; |
| 165 | + }, |
| 166 | + toBe() { |
| 167 | + return 4; |
| 168 | + }, |
| 169 | + }, |
| 170 | +]); |
31 | 171 | ``` |
32 | | -then you run in terminal. |
| 172 | + |
| 173 | +output |
33 | 174 |
|
34 | 175 | ```sh |
35 | 176 | $ deno test |
| 177 | + |
| 178 | +running 2 tests |
| 179 | +test object assignment ... ok (10ms) |
| 180 | +test two plus two is four ... ok (1ms) |
| 181 | + |
| 182 | +test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (13ms) |
| 183 | +``` |
| 184 | + |
| 185 | +### notEqual |
| 186 | + |
| 187 | +`example.test.ts` |
| 188 | + |
| 189 | +```typescript |
| 190 | +test.testNotEqual("two plus two not is five", { |
| 191 | + expect() { |
| 192 | + return 2 + 2; |
| 193 | + }, |
| 194 | + notBe() { |
| 195 | + return 4; |
| 196 | + }, |
| 197 | +}); |
36 | 198 | ``` |
37 | | -you should see this by console. |
| 199 | + |
| 200 | +output |
38 | 201 |
|
39 | 202 | ```sh |
| 203 | +$ deno test |
| 204 | + |
40 | 205 | running 1 tests |
41 | | -test 2 + 2 ... ok (15ms) |
| 206 | +test two plus two not is five ... FAILED (2ms) |
| 207 | + |
| 208 | +failures: |
| 209 | + |
| 210 | +two plus two not is five |
| 211 | +AssertionError: actual: 4 expected: 4 |
| 212 | + at assertNotEquals (https://deno.land/std/testing/asserts.ts:195:5) |
| 213 | + at fn (merlin.ts:105:9) |
| 214 | + at async asyncOpSanitizer ($deno$/testing.ts:34:5) |
| 215 | + at async Object.resourceSanitizer [as fn] ($deno$/testing.ts:68:5) |
| 216 | + at async TestRunner.[Symbol.asyncIterator] ($deno$/testing.ts:276:11) |
| 217 | + at async Object.runTests ($deno$/testing.ts:364:20) |
| 218 | + |
| 219 | +failures: |
| 220 | + |
| 221 | + two plus two not is five |
| 222 | + |
| 223 | +test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out (2ms) |
| 224 | +``` |
42 | 225 |
|
43 | | -test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (32ms) |
| 226 | +## stringContains |
| 227 | + |
| 228 | +`example.test.ts` |
| 229 | + |
| 230 | +```typescript |
| 231 | +test.stringContains("hello world contains orld", { |
| 232 | + Contains: () => "orld", |
| 233 | + value: () => "Hello World", |
| 234 | +}); |
| 235 | +``` |
| 236 | + |
| 237 | +```sh |
| 238 | +deno test |
| 239 | + |
| 240 | +test hello world contains orld ... ok (8ms) |
| 241 | + |
| 242 | +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out |
| 243 | +``` |
| 244 | + |
| 245 | +### fetchEqual |
| 246 | + |
| 247 | +```typescript |
| 248 | +test.fetchEqual("fetch data", { |
| 249 | + url: "https://jsonplaceholder.typicode.com/todos/1", |
| 250 | + type: "json", |
| 251 | + toBe() { |
| 252 | + return { userId: 1, id: 1, title: "delectus aut autem", completed: false }; |
| 253 | + }, |
| 254 | +}); |
| 255 | +``` |
| 256 | + |
| 257 | +```sh |
| 258 | +deno test |
| 259 | + |
| 260 | +test fetch data ... ok (1440ms) |
| 261 | + |
| 262 | +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out |
44 | 263 | ``` |
| 264 | + |
| 265 | +### testRegExp |
| 266 | +```typescript |
| 267 | +test.testRegExp("regEx match",{ |
| 268 | + expect:()=> "https://google.com", |
| 269 | + toBe:()=> new RegExp("^https?:\/\/[a-z.]+\.com$"), |
| 270 | +}) |
| 271 | +``` |
| 272 | +```sh |
| 273 | +deno test |
| 274 | + |
| 275 | +test regEx match ... ok (6ms) |
| 276 | + |
| 277 | +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (342ms) |
| 278 | +``` |
0 commit comments