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>[] = [];
|
const calls: SpyCall<Self, Args, Self>[] = [];
|
||||||
// @ts-ignore TS2509: Can't know the type of `original` statically.
|
// @ts-ignore TS2509: Can't know the type of `original` statically.
|
||||||
const spy = class extends original {
|
const spy = class extends original {
|
||||||
|
// deno-lint-ignore constructor-super
|
||||||
constructor(...args: Args) {
|
constructor(...args: Args) {
|
||||||
super(...args);
|
|
||||||
const call: SpyCall<Self, Args, Self> = { args };
|
const call: SpyCall<Self, Args, Self> = { args };
|
||||||
try {
|
try {
|
||||||
|
super(...args);
|
||||||
call.returned = this as unknown as Self;
|
call.returned = this as unknown as Self;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
call.error = error as Error;
|
call.error = error as Error;
|
||||||
|
@ -510,6 +510,21 @@ Deno.test("spy() works on constructor of child class", () => {
|
|||||||
assertSpyCalls(PointSpy, 1);
|
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", () => {
|
Deno.test("spy() works with throwing method", () => {
|
||||||
const obj = {
|
const obj = {
|
||||||
fn() {
|
fn() {
|
||||||
|
Loading…
Reference in New Issue
Block a user