Deno &JavaScript之测试

603 阅读1分钟

目标使用

  • 0. Deno 测试
  • 1. Jest API 以及使用方法
  • 2. Jest 与微信小程序
  • 3. Jest 与 Vue 测试
  • 4. Jest 与 React 测试

Deno 测试

测试风格:

  • 传统的测试风格,类似于 Jest 等等
  • 对象描述测试风格
  • 性能测试

传统的测试方法

API: Deno.test 函数

const { test } = Deno;
test('测试描述', () => {
    // 需要测试的代码
})
test("hello world", () => {
  const x = 1 + 2;
  if (x !== 3) {
    throw Error("x should be equal to 3");
  }
});

对象描述测试风格

import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
const { test } = Deno;

test({
  name: "testing example",
  fn(): void {
    assertEquals("world", "world");
    assertEquals({ hello: "world" }, { hello: "world" });
  },
});
  • 使用断言
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
const { test } = Deno;

test("hello world", () => {
  const x = 1 + 2;
  assertEquals(x, 3);
});

API列表

序号 断言 说明
1 equal() -
2 assert() -
3 assertEquals() -
4 assertNotEquals() -
5 assertStrictEq() -
6 assertStrContains() -
7 assertMatch() -
8 assertArrayContains() -
9 assertThrows() -
10 assertThrowsAsync() -
11 unimplemented() -
12 unreachable() -

Deno 性能测试

Deno 是一个比较完整的框架也好也好,周边都做的比较完整了,性能测试也成了其中的一环

序号 断言 说明
1 bench(benchmark) -
2 runBenchmarks(opts) -
3 runIfMain(meta, opts) -

参考