Skip to content
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

fix(testing): correctly throw in constructor with spy() #5139

Merged
merged 4 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion testing/mock.ts
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -759,10 +759,11 @@ function constructorSpy<
const calls: SpyCall<Self, Args, Self>[] = [];
// @ts-ignore TS2509: Can't know the type of `original` statically.
const spy = class extends original {
// deno-lint-ignore constructor-super
constructor(...args: Args) {
super(...args);
const call: SpyCall<Self, Args, Self> = { args };
try {
super(...args);
call.returned = this as unknown as Self;
} catch (error) {
call.error = error as Error;
Expand Down
15 changes: 15 additions & 0 deletions testing/mock_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,21 @@ Deno.test("spy() works on constructor of child class", () => {
assertSpyCalls(PointSpy, 1);
});

Deno.test("spy() works on constructor that throws an error", () => {
class Foo {
constructor() {
throw new Error("foo");
}
}
const FooSpy = spy(Foo);
assertThrows(() => new FooSpy(), Error, "foo");
assertSpyCall(FooSpy, 0, {
self: undefined,
args: [],
error: { Class: Error, msgIncludes: "foo" },
});
});

Deno.test("spy() works with throwing method", () => {
const obj = {
fn() {
Expand Down