-
Notifications
You must be signed in to change notification settings - Fork 623
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(expect): support
expect.hasAssertions()
(#5901)
Co-authored-by: Yoshiya Hinosawa <[email protected]>
- Loading branch information
Showing
9 changed files
with
221 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { getAssertionState } from "@std/internal/assertion-state"; | ||
|
||
const assertionState = getAssertionState(); | ||
|
||
export function hasAssertions() { | ||
assertionState.setAssertionCheck(true); | ||
} | ||
|
||
export function emitAssertionTrigger() { | ||
assertionState.setAssertionTriggered(true); | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. 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 throw `toThrowErrorMatchingSnapshot` | ||
it("should throw an error", () => { | ||
expect.hasAssertions(); | ||
}); | ||
|
||
it("should pass", () => { | ||
expect.hasAssertions(); | ||
expect("a").toEqual("a"); | ||
}); | ||
}); | ||
|
||
it("it() suite should pass", () => { | ||
expect.hasAssertions(); | ||
expect("a").toEqual("a"); | ||
}); | ||
|
||
// FIXME(eryue0220): This test should throw `toThrowErrorMatchingSnapshot` | ||
test("test suite should throw an error", () => { | ||
expect.hasAssertions(); | ||
}); | ||
|
||
test("test suite should pass", () => { | ||
expect.hasAssertions(); | ||
expect("a").toEqual("a"); | ||
}); | ||
}); |
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
|
||
/** | ||
* Check the test suite internal state | ||
* | ||
* @example Usage | ||
* ```ts no-eval | ||
* import { AssertionState } from "@std/internal"; | ||
* | ||
* const assertionState = new AssertionState(); | ||
* ``` | ||
*/ | ||
export class AssertionState { | ||
#state: { | ||
assertionCheck: boolean; | ||
assertionTriggered: boolean; | ||
}; | ||
|
||
constructor() { | ||
this.#state = { | ||
assertionCheck: false, | ||
assertionTriggered: false, | ||
}; | ||
} | ||
|
||
/** | ||
* If `expect.hasAssertions` called, then through this method to update #state.assertionCheck value. | ||
* | ||
* @param val Set #state.assertionCheck's value | ||
* | ||
* @example Usage | ||
* ```ts no-eval | ||
* import { AssertionState } from "@std/internal"; | ||
* | ||
* const assertionState = new AssertionState(); | ||
* assertionState.setAssertionCheck(true); | ||
* ``` | ||
*/ | ||
setAssertionCheck(val: boolean) { | ||
this.#state.assertionCheck = val; | ||
} | ||
|
||
/** | ||
* If any matchers was called, `#state.assertionTriggered` will be set through this method. | ||
* | ||
* @param val Set #state.assertionTriggered's value | ||
* | ||
* @example Usage | ||
* ```ts no-eval | ||
* import { AssertionState } from "@std/internal"; | ||
* | ||
* const assertionState = new AssertionState(); | ||
* assertionState.setAssertionTriggered(true); | ||
* ``` | ||
*/ | ||
setAssertionTriggered(val: boolean) { | ||
this.#state.assertionTriggered = val; | ||
} | ||
|
||
/** | ||
* Check Assertion internal state, if `#state.assertionCheck` is set true, but | ||
* `#state.assertionTriggered` is still false, then should throw an Assertion Error. | ||
* | ||
* @returns a boolean value, that the test suite is satisfied with the check. If not, | ||
* it should throw an AssertionError. | ||
* | ||
* @example Usage | ||
* ```ts no-eval | ||
* import { AssertionState } from "@std/internal"; | ||
* | ||
* const assertionState = new AssertionState(); | ||
* if (assertionState.checkAssertionErrorStateAndReset()) { | ||
* // throw AssertionError(""); | ||
* } | ||
* ``` | ||
*/ | ||
checkAssertionErrorStateAndReset(): boolean { | ||
const result = this.#state.assertionCheck && | ||
!this.#state.assertionTriggered; | ||
|
||
this.#resetAssertionState(); | ||
|
||
return result; | ||
} | ||
|
||
#resetAssertionState(): void { | ||
this.#state = { | ||
assertionCheck: false, | ||
assertionTriggered: false, | ||
}; | ||
} | ||
} | ||
|
||
const assertionState = new AssertionState(); | ||
|
||
/** | ||
* return an instance of AssertionState | ||
* | ||
* @returns AssertionState | ||
* | ||
* @example Usage | ||
* ```ts no-eval | ||
* import { getAssertionState } from "@std/internal"; | ||
* | ||
* const assertionState = getAssertionState(); | ||
* assertionState.setAssertionTriggered(true); | ||
* ``` | ||
*/ | ||
export function getAssertionState(): AssertionState { | ||
return assertionState; | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { assertEquals } from "@std/assert"; | ||
import { AssertionState } from "./assertion_state.ts"; | ||
|
||
Deno.test("AssertionState checkAssertionErrorStateAndReset pass", () => { | ||
const assertionState = new AssertionState(); | ||
assertionState.setAssertionTriggered(true); | ||
|
||
assertEquals(assertionState.checkAssertionErrorStateAndReset(), false); | ||
}); | ||
|
||
Deno.test("AssertionState checkAssertionErrorStateAndReset pass", () => { | ||
const assertionState = new AssertionState(); | ||
assertionState.setAssertionTriggered(true); | ||
|
||
assertEquals(assertionState.checkAssertionErrorStateAndReset(), false); | ||
|
||
assertionState.setAssertionCheck(true); | ||
assertEquals(assertionState.checkAssertionErrorStateAndReset(), true); | ||
}); | ||
|
||
Deno.test("AssertionState checkAssertionErrorStateAndReset fail", () => { | ||
const assertionState = new AssertionState(); | ||
assertionState.setAssertionCheck(true); | ||
|
||
assertEquals(assertionState.checkAssertionErrorStateAndReset(), true); | ||
}); |
This file contains 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 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 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