mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +00:00
fix(testing): correctly throw in constructor with spy()
(#5139)
This commit is contained in:
parent
040e666033
commit
06d43901a8
@ -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;
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user