-
Notifications
You must be signed in to change notification settings - Fork 653
fix(expect,testing,internal): throw if expect.hasAssertion
and expect.assertions
are not checked
#6646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
fix(expect,testing,internal): throw if expect.hasAssertion
and expect.assertions
are not checked
#6646
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,180 @@ | ||
// Copyright 2018-2025 the Deno authors. MIT license. | ||
|
||
import { describe, it, test } from "@std/testing/bdd"; | ||
import { expect } from "./expect.ts"; | ||
|
||
Deno.test("expect.hasAssertions() API", () => { | ||
describe("describe suite", () => { | ||
// FIXME(eryue0220): This test should through `toThrowErrorMatchingSnapshot` | ||
it("should throw an error", () => { | ||
expect.hasAssertions(); | ||
}); | ||
import * as path from "@std/path"; | ||
import { assertStringIncludes } from "@std/assert"; | ||
import { stripAnsiCode } from "@std/internal/styles"; | ||
|
||
it("should pass", () => { | ||
expect.hasAssertions(); | ||
expect("a").toEqual("a"); | ||
}); | ||
}); | ||
|
||
it("it() suite should pass", () => { | ||
expect.hasAssertions(); | ||
expect("a").toEqual("a"); | ||
Deno.test("expect.hasAssertions() API", async () => { | ||
const tempDirPath = await Deno.makeTempDir({ | ||
prefix: "deno_std_has_assertions_", | ||
}); | ||
try { | ||
const tempFilePath = path.join(tempDirPath, "has_assertions_test.ts"); | ||
await Deno.writeTextFile( | ||
tempFilePath, | ||
`import { describe, it, test } from "@std/testing/bdd"; | ||
import { expect } from "@std/expect"; | ||
|
||
// FIXME(eryue0220): This test should through `toThrowErrorMatchingSnapshot` | ||
test("test suite should throw an error", () => { | ||
describe("describe suite", () => { | ||
it("describe should throw an error", () => { | ||
expect.hasAssertions(); | ||
}); | ||
|
||
test("test suite should pass", () => { | ||
it("describe should pass", () => { | ||
expect.hasAssertions(); | ||
expect("a").toEqual("a"); | ||
}); | ||
}); | ||
|
||
Deno.test("expect.assertions() API", () => { | ||
test("should pass", () => { | ||
expect.assertions(2); | ||
expect("a").not.toBe("b"); | ||
expect("a").toBe("a"); | ||
}); | ||
it("it() suite should throw an error", () => { | ||
expect.hasAssertions(); | ||
}); | ||
|
||
// FIXME(eryue0220): This test should through `toThrowErrorMatchingSnapshot` | ||
test("should throw error", () => { | ||
expect.assertions(1); | ||
expect("a").not.toBe("b"); | ||
expect("a").toBe("a"); | ||
}); | ||
it("it() suite should pass", () => { | ||
expect.hasAssertions(); | ||
expect("a").toEqual("a"); | ||
}); | ||
|
||
it("redeclare different assertion count", () => { | ||
expect.assertions(3); | ||
expect("a").not.toBe("b"); | ||
expect("a").toBe("a"); | ||
expect.assertions(2); | ||
}); | ||
test("test suite should throw an error", () => { | ||
expect.hasAssertions(); | ||
}); | ||
|
||
test("test suite should pass", () => { | ||
expect.hasAssertions(); | ||
expect("a").toEqual("a"); | ||
}); | ||
`, | ||
); | ||
|
||
const command = new Deno.Command(Deno.execPath(), { | ||
args: ["test", "--no-lock", tempDirPath], | ||
}); | ||
const { stdout } = await command.output(); | ||
const errorMessage = stripAnsiCode(new TextDecoder().decode(stdout)); | ||
|
||
assertStringIncludes( | ||
errorMessage, | ||
"describe should throw an error ... FAILED", | ||
); | ||
assertStringIncludes(errorMessage, "describe should pass ... ok"); | ||
assertStringIncludes( | ||
errorMessage, | ||
"it() suite should throw an error ... FAILED", | ||
); | ||
assertStringIncludes(errorMessage, "it() suite should pass ... ok"); | ||
assertStringIncludes( | ||
errorMessage, | ||
"test suite should throw an error ... FAILED", | ||
); | ||
assertStringIncludes(errorMessage, "test suite should pass ... ok"); | ||
|
||
test("expect no assertions", () => { | ||
expect.assertions(0); | ||
assertStringIncludes( | ||
errorMessage, | ||
"error: AssertionError: Expected at least one assertion to be called but received none", | ||
); | ||
} finally { | ||
await Deno.remove(tempDirPath, { recursive: true }); | ||
} | ||
}); | ||
|
||
Deno.test("expect.assertions() API", async () => { | ||
const tempDirPath = await Deno.makeTempDir({ | ||
prefix: "deno_std_has_assertions_", | ||
}); | ||
try { | ||
const tempFilePath = path.join(tempDirPath, "has_assertions_test.ts"); | ||
await Deno.writeTextFile( | ||
tempFilePath, | ||
`import { describe, it, test } from "@std/testing/bdd"; | ||
import { expect } from "@std/expect"; | ||
|
||
// FIXME(eryue0220): This test should through `toThrowErrorMatchingSnapshot` | ||
it("should throw an error", () => { | ||
expect.assertions(2); | ||
test("should pass", () => { | ||
expect.assertions(2); | ||
expect("a").not.toBe("b"); | ||
expect("a").toBe("a"); | ||
}); | ||
|
||
test("should throw error", () => { | ||
expect.assertions(1); | ||
expect("a").not.toBe("b"); | ||
expect("a").toBe("a"); | ||
}); | ||
|
||
it("redeclare different assertion count", () => { | ||
expect.assertions(3); | ||
expect("a").not.toBe("b"); | ||
expect("a").toBe("a"); | ||
expect.assertions(2); | ||
}); | ||
|
||
test("expect no assertions", () => { | ||
expect.assertions(0); | ||
}); | ||
|
||
it("should throw an error", () => { | ||
expect.assertions(2); | ||
}); | ||
`, | ||
); | ||
|
||
const command = new Deno.Command(Deno.execPath(), { | ||
args: ["test", "--no-lock", tempDirPath], | ||
}); | ||
const { stdout } = await command.output(); | ||
const errorMessage = stripAnsiCode(new TextDecoder().decode(stdout)); | ||
|
||
assertStringIncludes(errorMessage, "should pass ... ok"); | ||
assertStringIncludes(errorMessage, "should throw error ... FAILED"); | ||
assertStringIncludes( | ||
errorMessage, | ||
"redeclare different assertion count ... ok", | ||
); | ||
assertStringIncludes(errorMessage, "expect no assertions ... ok"); | ||
assertStringIncludes(errorMessage, "should throw an error ... FAILED"); | ||
|
||
assertStringIncludes( | ||
errorMessage, | ||
"error: AssertionError: Expected exactly 1 assertion to be called, but received 2", | ||
); | ||
assertStringIncludes( | ||
errorMessage, | ||
"error: AssertionError: Expected exactly 2 assertions to be called, but received 0", | ||
); | ||
} finally { | ||
await Deno.remove(tempDirPath, { recursive: true }); | ||
} | ||
}); | ||
|
||
Deno.test("expect assertions reset after errored tests", async () => { | ||
const tempDirPath = await Deno.makeTempDir({ | ||
prefix: "deno_std_assertions_reset_", | ||
}); | ||
try { | ||
const tempFilePath = path.join(tempDirPath, "assertion_reset_test.ts"); | ||
await Deno.writeTextFile( | ||
tempFilePath, | ||
`import { it } from "@std/testing/bdd"; | ||
import { expect } from "@std/expect"; | ||
it("should fail", () => { | ||
expect.assertions(2); | ||
expect(1).toBe(1); | ||
}); | ||
it("should pass", () => { | ||
expect.assertions(0); | ||
}); | ||
`, | ||
); | ||
|
||
const command = new Deno.Command(Deno.execPath(), { | ||
args: ["test", "--no-lock", tempDirPath], | ||
}); | ||
const { stdout } = await command.output(); | ||
const errorMessage = stripAnsiCode(new TextDecoder().decode(stdout)); | ||
|
||
assertStringIncludes(errorMessage, "should fail ... FAILED"); | ||
// Previously "should fail" failing caused "should pass" to fail | ||
assertStringIncludes(errorMessage, "should pass ... ok"); | ||
} finally { | ||
await Deno.remove(tempDirPath, { recursive: true }); | ||
} | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,56 @@ | ||
// Copyright 2018-2025 the Deno authors. MIT license. | ||
|
||
import { assertEquals } from "@std/assert"; | ||
import { assertEquals, assertStringIncludes } from "@std/assert"; | ||
import { AssertionState } from "./assertion_state.ts"; | ||
import { stripAnsiCode } from "./styles.ts"; | ||
|
||
Deno.test("AssertionState checkAssertionErrorState pass", () => { | ||
const assertionState = new AssertionState(); | ||
assertionState.setAssertionTriggered(true); | ||
|
||
assertEquals(assertionState.checkAssertionErrorState(), false); | ||
try { | ||
assertionState.setAssertionTriggered(true); | ||
assertEquals(assertionState.checkAssertionErrorState(), false); | ||
} finally { | ||
assertionState.resetAssertionState(); | ||
} | ||
}); | ||
|
||
Deno.test("AssertionState checkAssertionErrorState pass", () => { | ||
const assertionState = new AssertionState(); | ||
assertionState.setAssertionTriggered(true); | ||
try { | ||
assertionState.setAssertionTriggered(true); | ||
|
||
assertEquals(assertionState.checkAssertionErrorState(), false); | ||
assertEquals(assertionState.checkAssertionErrorState(), false); | ||
|
||
assertionState.setAssertionCheck(true); | ||
assertEquals(assertionState.checkAssertionErrorState(), false); | ||
assertionState.setAssertionCheck(true); | ||
assertEquals(assertionState.checkAssertionErrorState(), false); | ||
} finally { | ||
assertionState.resetAssertionState(); | ||
} | ||
}); | ||
|
||
Deno.test("AssertionState checkAssertionErrorState fail", () => { | ||
const assertionState = new AssertionState(); | ||
assertionState.setAssertionCheck(true); | ||
try { | ||
assertionState.setAssertionCheck(true); | ||
assertEquals(assertionState.checkAssertionErrorState(), true); | ||
} finally { | ||
assertionState.resetAssertionState(); | ||
} | ||
}); | ||
|
||
assertEquals(assertionState.checkAssertionErrorState(), true); | ||
Deno.test("AssertionState throws if not cleaned up", async () => { | ||
const command = new Deno.Command(Deno.execPath(), { | ||
args: [ | ||
"eval", | ||
` | ||
import { AssertionState } from "@std/internal/assertion-state"; | ||
const assertionState = new AssertionState(); | ||
assertionState.setAssertionCount(0); | ||
`, | ||
], | ||
}); | ||
const { stderr } = await command.output(); | ||
const errorMessage = stripAnsiCode(new TextDecoder().decode(stderr)); | ||
// TODO(WWRS): Test for the expected message when Deno displays it instead of "Uncaught null" | ||
assertStringIncludes(errorMessage, "error: Uncaught"); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this supposed to be the message generated in
unload
handler?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think this is an upstream issue, which I've opened: denoland/deno#29087