summaryrefslogtreecommitdiff
path: root/tests/unit_node/assertion_error_test.ts
blob: 4f8fe70ba55286cc8326665b77e9907cc3f24d8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { stripAnsiCode } from "@std/fmt/colors";
import { assert, assertStrictEquals } from "@std/assert";
import { AssertionError } from "node:assert";

Deno.test({
  name: "construct AssertionError() with given message",
  fn() {
    const err = new AssertionError(
      {
        message: "answer",
        actual: "42",
        expected: "42",
        operator: "notStrictEqual",
      },
    );
    assertStrictEquals(err.name, "AssertionError");
    assertStrictEquals(err.message, "answer");
    assertStrictEquals(err.generatedMessage, false);
    assertStrictEquals(err.code, "ERR_ASSERTION");
    assertStrictEquals(err.actual, "42");
    assertStrictEquals(err.expected, "42");
    assertStrictEquals(err.operator, "notStrictEqual");
  },
});

Deno.test({
  name: "construct AssertionError() with generated message",
  fn() {
    const err = new AssertionError(
      { actual: 1, expected: 2, operator: "equal" },
    );
    assertStrictEquals(err.name, "AssertionError");
    assertStrictEquals(stripAnsiCode(err.message), "1 equal 2");
    assertStrictEquals(err.generatedMessage, true);
    assertStrictEquals(err.code, "ERR_ASSERTION");
    assertStrictEquals(err.actual, 1);
    assertStrictEquals(err.expected, 2);
    assertStrictEquals(err.operator, "equal");
  },
});

Deno.test({
  name: "construct AssertionError() with stackStartFn",
  fn: function stackStartFn() {
    const expected = /node/;
    const err = new AssertionError({
      actual: "deno",
      expected,
      operator: "match",
      stackStartFn,
    });
    assertStrictEquals(err.name, "AssertionError");
    assertStrictEquals(stripAnsiCode(err.message), `'deno' match /node/`);
    assertStrictEquals(err.generatedMessage, true);
    assertStrictEquals(err.code, "ERR_ASSERTION");
    assertStrictEquals(err.actual, "deno");
    assertStrictEquals(err.expected, expected);
    assertStrictEquals(err.operator, "match");
    assert(err.stack, "error should have a stack");
    assert(
      !err.stack?.includes("stackStartFn"),
      "stackStartFn() should not present in stack trace",
    );
  },
});