测试

Deno 内置了测试JavaScript 和TypeScript 代码的测试运行器。

写测试

你需要调用 Deno.test 的时候带上要测试点的名字和对应的测试函数:

  1. Deno.test("hello world", () => {
  2. const x = 1 + 2;
  3. if (x !== 3) {
  4. throw Error("x should be equal to 3");
  5. }
  6. });

https://deno.land/std/testing 有一些有用的断言库可以让测试更简单:

  1. import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
  2. Deno.test("hello world", () => {
  3. const x = 1 + 2;
  4. assertEquals(x, 3);
  5. });

Async 函数

如果测试函数中返回promise可以测试异步代码,这样你可以在定义函数的时候使用 async 关键字:

  1. Deno.test("async hello world", async () => {
  2. const x = 1 + 2;
  3. // await some async task
  4. await delay(100);
  5. if (x !== 3) {
  6. throw Error("x should be equal to 3");
  7. }
  8. });

Resource and async op sanitizers

Deno 的某些行为会在资源表中创建资源(或许更多内容),这些资源在使用完之后必须要关掉。

测试运行器会检查每个测试定义中创建的所有资源都被关闭。这样做是为了防止资源泄漏。 所有测试默认开启检查,但可通过设置布尔值 sanitizeResources 为false 来禁用。

对于类似文件系统交互的异步操作也是这样的。测试运行器检查测试开始到测试结束前都每个操作都是闭环的。 所有测试默认开启检查,但可通过设置布尔值 sanitizeOps 为false 来禁用。

  1. Deno.test({
  2. name: "leaky test",
  3. fn() {
  4. Deno.open("hello.txt");
  5. },
  6. sanitizeResources: false,
  7. sanitizeOps: false,
  8. });

忽略测试

有时候你可能需要在某些情况下忽略测试(比如你只想在Windows 下运行测试)。这样你可以在测试定义中设置布尔值 ignore ,为true时忽略。

  1. Deno.test({
  2. name: "do macOS feature",
  3. ignore: Deno.build.os !== "darwin",
  4. fn() {
  5. doMacOSFeature();
  6. },
  7. });

运行测试

运行测试调用 deno test 带上包含测试函数的文件:

  1. deno test my_test.ts

你也可以省略文件名,当前目录及其子目录下的所有 {*_,}test.{js,ts,jsx,tsx} 文件中的测试都会被执行。 如果你传了一个目录,那么这个目录下的所有匹配 {*_,}test.{js,ts,jsx,tsx} 的文件都会被执行。