DAY 23
3
Modern Web

# 強型闖入DenoLand[22] - Testing and Related Tools(3)

## 進入正題

``````import { assert } from "https://deno.land/std@0.73.0/testing/asserts.ts";

Deno.test("Hello Test", () => {
assert("Hello");
});
``````

`assert()` 是一個簡單的布林值斷言，可以用於斷言任何可推導成 `true` 的值。

### 相等性

Deno 提供了三種相等性斷言供開發者使用：

• `assertEquals(para1, para2, errMsg)`

比較兩者是否相等。

• `assertNotEquals(para1, para2, errMsg)`

比較兩者是否為不相等。

• `assertStrictEquals(para1, para2, errMsg)`

該斷言方法不會對比較值進行強制轉型，這也就意味著使用者需要確保兩個比較輸入值的型別是完全相同的。

``````// 受測邏輯
export function sum(para1: number, para2: number): number{
return para1 + para2 ;
}
``````
``````// 測試程式
import { assertEquals } from "https://deno.land/std@0.73.0/testing/asserts.ts";
import { sum } from "./testLogi.ts";
Deno.test("check on sum",()=>{
let result = sum(1,2)
assertEquals(result,4,`result should be 4, but output is \${result}.`);
});
``````

``````deno test test.ts
``````

``````test check on sum ... FAILED (2ms)

failures:

check on sum
AssertionError: result should be 4, but output is 3.
at assertEquals (asserts.ts:196:9)
at test.ts:12:5
at asyncOpSanitizer (deno:cli/rt/40_testing.js:34:13)
at Object.resourceSanitizer [as fn] (deno:cli/rt/40_testing.js:68:13)
at TestRunner.[Symbol.asyncIterator] (deno:cli/rt/40_testing.js:240:24)
at AsyncGenerator.next (<anonymous>)
at Object.runTests (deno:cli/rt/40_testing.js:317:22)

failures:

check on sum

test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out (4ms)
``````

``````import { assertEquals } from "https://deno.land/std@0.73.0/testing/asserts.ts";
import {sum} from "./testLogi.ts";
Deno.test("check on foo", () => {
class Foo {}
const foo1 = new Foo();
const foo2 = new Foo();
assertEquals(foo1, foo2, "not eq");
});
Deno.test("check on sum",()=>{
let result = sum(2,2)
assertEquals(result,4,`result should be 4, but output is \${result}.`);
});
``````

``````running 2 tests
test check on foo ... ok (3ms)
test check on sum ... ok (1ms)

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (4ms)
``````

### 選擇單一測試

``````deno test --filter "check on sum" test.ts
``````

``````running 1 tests
test check on sum ... ok (4ms)

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out (5ms)
``````

## 其他斷言

Deno 一共提供了 10 種斷言方法：

• `assert(expr: unknown, msg = ""): asserts expr`
• `assertEquals(actual: unknown, expected: unknown, msg?: string): void`
• `assertNotEquals(actual: unknown, expected: unknown, msg?: string): void`
• `assertStrictEquals(actual: unknown, expected: unknown, msg?: string): void`
• `assertStringContains(actual: string, expected: string, msg?: string): void`
• `assertArrayContains(actual: unknown[], expected: unknown[], msg?: string): void`
• `assertMatch(actual: string, expected: RegExp, msg?: string): void`
• `assertNotMatch(actual: string, expected: RegExp, msg?: string): void`
• `assertThrows(fn: () => void, ErrorClass?: Constructor, msgIncludes = "", msg?: string): Error`
• `assertThrowsAsync(fn: () => Promise<void>, ErrorClass?: Constructor, msgIncludes = "", msg?: string): Promise<Error>`