|
1 | 1 | // Copyright 2018-2025 the Deno authors. MIT license.
|
2 | 2 |
|
3 | 3 | import { describe, it, test } from "@std/testing/bdd";
|
| 4 | +import * as path from "@std/path"; |
| 5 | +import { assertStringIncludes } from "@std/assert"; |
4 | 6 | import { expect } from "./expect.ts";
|
5 | 7 |
|
6 |
| -Deno.test("expect.hasAssertions() API", () => { |
7 |
| - describe("describe suite", () => { |
8 |
| - // FIXME(eryue0220): This test should through `toThrowErrorMatchingSnapshot` |
9 |
| - it("should throw an error", () => { |
10 |
| - expect.hasAssertions(); |
11 |
| - }); |
12 |
| - |
13 |
| - it("should pass", () => { |
14 |
| - expect.hasAssertions(); |
15 |
| - expect("a").toEqual("a"); |
16 |
| - }); |
17 |
| - }); |
18 |
| - |
19 |
| - it("it() suite should pass", () => { |
20 |
| - expect.hasAssertions(); |
21 |
| - expect("a").toEqual("a"); |
| 8 | +Deno.test("expect.hasAssertions() API", async () => { |
| 9 | + const tempDirPath = await Deno.makeTempDir({ |
| 10 | + prefix: "deno_std_has_assertions_", |
22 | 11 | });
|
| 12 | + try { |
| 13 | + const tempFilePath = path.join(tempDirPath, "has_assertions_test.ts"); |
| 14 | + await Deno.writeTextFile( |
| 15 | + tempFilePath, |
| 16 | + `import { describe, it, test } from "@std/testing/bdd"; |
| 17 | +import { expect } from "@std/expect"; |
23 | 18 |
|
24 |
| - // FIXME(eryue0220): This test should through `toThrowErrorMatchingSnapshot` |
25 |
| - test("test suite should throw an error", () => { |
| 19 | +describe("describe suite", () => { |
| 20 | + it("describe should throw an error", () => { |
26 | 21 | expect.hasAssertions();
|
27 | 22 | });
|
28 | 23 |
|
29 |
| - test("test suite should pass", () => { |
| 24 | + it("describe should pass", () => { |
30 | 25 | expect.hasAssertions();
|
31 | 26 | expect("a").toEqual("a");
|
32 | 27 | });
|
33 | 28 | });
|
34 | 29 |
|
35 |
| -Deno.test("expect.assertions() API", () => { |
36 |
| - test("should pass", () => { |
37 |
| - expect.assertions(2); |
38 |
| - expect("a").not.toBe("b"); |
39 |
| - expect("a").toBe("a"); |
40 |
| - }); |
| 30 | +it("it() suite should throw an error", () => { |
| 31 | + expect.hasAssertions(); |
| 32 | +}); |
41 | 33 |
|
42 |
| - // FIXME(eryue0220): This test should through `toThrowErrorMatchingSnapshot` |
43 |
| - test("should throw error", () => { |
44 |
| - expect.assertions(1); |
45 |
| - expect("a").not.toBe("b"); |
46 |
| - expect("a").toBe("a"); |
47 |
| - }); |
| 34 | +it("it() suite should pass", () => { |
| 35 | + expect.hasAssertions(); |
| 36 | + expect("a").toEqual("a"); |
| 37 | +}); |
48 | 38 |
|
49 |
| - it("redeclare different assertion count", () => { |
50 |
| - expect.assertions(3); |
51 |
| - expect("a").not.toBe("b"); |
52 |
| - expect("a").toBe("a"); |
53 |
| - expect.assertions(2); |
54 |
| - }); |
| 39 | +test("test suite should throw an error", () => { |
| 40 | + expect.hasAssertions(); |
| 41 | +}); |
| 42 | +
|
| 43 | +test("test suite should pass", () => { |
| 44 | + expect.hasAssertions(); |
| 45 | + expect("a").toEqual("a"); |
| 46 | +}); |
| 47 | +`, |
| 48 | + ); |
| 49 | + |
| 50 | + const command = new Deno.Command(Deno.execPath(), { |
| 51 | + args: ["test", "--no-lock", tempDirPath], |
| 52 | + }); |
| 53 | + const { stdout } = await command.output(); |
| 54 | + const errorMessage = new TextDecoder().decode(stdout); |
| 55 | + |
| 56 | + assertStringIncludes( |
| 57 | + errorMessage, |
| 58 | + "describe should throw an error ... FAILED", |
| 59 | + ); |
| 60 | + assertStringIncludes(errorMessage, "describe should pass ... ok"); |
| 61 | + assertStringIncludes( |
| 62 | + errorMessage, |
| 63 | + "it() suite should throw an error ... FAILED", |
| 64 | + ); |
| 65 | + assertStringIncludes(errorMessage, "it() suite should pass ... ok"); |
| 66 | + assertStringIncludes( |
| 67 | + errorMessage, |
| 68 | + "test suite should throw an error ... FAILED", |
| 69 | + ); |
| 70 | + assertStringIncludes(errorMessage, "test suite should pass ... ok"); |
| 71 | + |
| 72 | + assertStringIncludes( |
| 73 | + errorMessage, |
| 74 | + "error: AssertionError: Expected at least one assertion to be called but received none", |
| 75 | + ); |
| 76 | + } finally { |
| 77 | + await Deno.remove(tempDirPath, { recursive: true }); |
| 78 | + } |
| 79 | +}); |
55 | 80 |
|
56 |
| - test("expect no assertions", () => { |
57 |
| - expect.assertions(0); |
| 81 | +Deno.test("expect.assertions() API", async () => { |
| 82 | + const tempDirPath = await Deno.makeTempDir({ |
| 83 | + prefix: "deno_std_has_assertions_", |
58 | 84 | });
|
| 85 | + try { |
| 86 | + const tempFilePath = path.join(tempDirPath, "has_assertions_test.ts"); |
| 87 | + await Deno.writeTextFile( |
| 88 | + tempFilePath, |
| 89 | + `import { describe, it, test } from "@std/testing/bdd"; |
| 90 | +import { expect } from "@std/expect"; |
59 | 91 |
|
60 |
| - // FIXME(eryue0220): This test should through `toThrowErrorMatchingSnapshot` |
61 |
| - it("should throw an error", () => { |
62 |
| - expect.assertions(2); |
| 92 | +test("should pass", () => { |
| 93 | + expect.assertions(2); |
| 94 | + expect("a").not.toBe("b"); |
| 95 | + expect("a").toBe("a"); |
| 96 | +}); |
| 97 | +
|
| 98 | +test("should throw error", () => { |
| 99 | + expect.assertions(1); |
| 100 | + expect("a").not.toBe("b"); |
| 101 | + expect("a").toBe("a"); |
| 102 | +}); |
| 103 | +
|
| 104 | +it("redeclare different assertion count", () => { |
| 105 | + expect.assertions(3); |
| 106 | + expect("a").not.toBe("b"); |
| 107 | + expect("a").toBe("a"); |
| 108 | + expect.assertions(2); |
| 109 | +}); |
| 110 | +
|
| 111 | +test("expect no assertions", () => { |
| 112 | + expect.assertions(0); |
| 113 | +}); |
| 114 | +
|
| 115 | +it("should throw an error", () => { |
| 116 | + expect.assertions(2); |
| 117 | +}); |
| 118 | +`, |
| 119 | + ); |
| 120 | + |
| 121 | + const command = new Deno.Command(Deno.execPath(), { |
| 122 | + args: ["test", "--no-lock", tempDirPath], |
| 123 | + }); |
| 124 | + const { stdout } = await command.output(); |
| 125 | + const errorMessage = new TextDecoder().decode(stdout); |
| 126 | + |
| 127 | + assertStringIncludes(errorMessage, "should pass ... ok"); |
| 128 | + assertStringIncludes(errorMessage, "should throw error ... FAILED"); |
| 129 | + assertStringIncludes( |
| 130 | + errorMessage, |
| 131 | + "redeclare different assertion count ... ok", |
| 132 | + ); |
| 133 | + assertStringIncludes(errorMessage, "expect no assertions ... ok"); |
| 134 | + assertStringIncludes(errorMessage, "should throw an error ... FAILED"); |
| 135 | + |
| 136 | + assertStringIncludes( |
| 137 | + errorMessage, |
| 138 | + "error: AssertionError: Expected exactly 1 assertion to be called, but received 2", |
| 139 | + ); |
| 140 | + assertStringIncludes( |
| 141 | + errorMessage, |
| 142 | + "error: AssertionError: Expected exactly 2 assertions to be called, but received 0", |
| 143 | + ); |
| 144 | + } finally { |
| 145 | + await Deno.remove(tempDirPath, { recursive: true }); |
| 146 | + } |
| 147 | +}); |
| 148 | + |
| 149 | +Deno.test("expect assertions reset after errored tests", async () => { |
| 150 | + const tempDirPath = await Deno.makeTempDir({ |
| 151 | + prefix: "deno_std_assertions_reset_", |
63 | 152 | });
|
| 153 | + try { |
| 154 | + const tempFilePath = path.join(tempDirPath, "assertion_reset_test.ts"); |
| 155 | + await Deno.writeTextFile( |
| 156 | + tempFilePath, |
| 157 | + `import { it } from "@std/testing/bdd"; |
| 158 | +import { expect } from "@std/expect"; |
| 159 | +it("should fail", () => { |
| 160 | + expect.assertions(2); |
| 161 | + expect(1).toBe(1); |
| 162 | +}); |
| 163 | +it("should pass", () => { |
| 164 | + expect.assertions(0); |
| 165 | +}); |
| 166 | +`, |
| 167 | + ); |
| 168 | + |
| 169 | + const command = new Deno.Command(Deno.execPath(), { |
| 170 | + args: ["test", "--no-lock", tempDirPath], |
| 171 | + }); |
| 172 | + const { stdout } = await command.output(); |
| 173 | + const errorMessage = new TextDecoder().decode(stdout); |
| 174 | + |
| 175 | + assertStringIncludes(errorMessage, "should fail ... FAILED"); |
| 176 | + // Previously "should fail" failing caused "should pass" to fail |
| 177 | + assertStringIncludes(errorMessage, "should pass ... ok"); |
| 178 | + } finally { |
| 179 | + await Deno.remove(tempDirPath, { recursive: true }); |
| 180 | + } |
64 | 181 | });
|
0 commit comments