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