mirror of
https://github.com/denoland/deno.git
synced 2025-03-09 13:49:37 -04:00
fix(std/testing): assertThrows gracefully fails if non-Error thrown (#6330)
This commit is contained in:
parent
d2403caa3b
commit
d615f0ff82
2 changed files with 96 additions and 1 deletions
|
@ -322,7 +322,8 @@ export function fail(msg?: string): void {
|
||||||
assert(false, `Failed assertion${msg ? `: ${msg}` : "."}`);
|
assert(false, `Failed assertion${msg ? `: ${msg}` : "."}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Executes a function, expecting it to throw. If it does not, then it
|
/**
|
||||||
|
* Executes a function, expecting it to throw. If it does not, then it
|
||||||
* throws. An error class and a string that should be included in the
|
* throws. An error class and a string that should be included in the
|
||||||
* error message can also be asserted.
|
* error message can also be asserted.
|
||||||
*/
|
*/
|
||||||
|
@ -337,6 +338,9 @@ export function assertThrows<T = void>(
|
||||||
try {
|
try {
|
||||||
fn();
|
fn();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
if (e instanceof Error === false) {
|
||||||
|
throw new AssertionError("A non-Error object was thrown.");
|
||||||
|
}
|
||||||
if (ErrorClass && !(Object.getPrototypeOf(e) === ErrorClass.prototype)) {
|
if (ErrorClass && !(Object.getPrototypeOf(e) === ErrorClass.prototype)) {
|
||||||
msg = `Expected error to be instance of "${ErrorClass.name}", but was "${
|
msg = `Expected error to be instance of "${ErrorClass.name}", but was "${
|
||||||
e.constructor.name
|
e.constructor.name
|
||||||
|
@ -362,6 +366,11 @@ export function assertThrows<T = void>(
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes a function which returns a promise, expecting it to throw or reject.
|
||||||
|
* If it does not, then it throws. An error class and a string that should be
|
||||||
|
* included in the error message can also be asserted.
|
||||||
|
*/
|
||||||
export async function assertThrowsAsync<T = void>(
|
export async function assertThrowsAsync<T = void>(
|
||||||
fn: () => Promise<T>,
|
fn: () => Promise<T>,
|
||||||
ErrorClass?: Constructor,
|
ErrorClass?: Constructor,
|
||||||
|
@ -373,6 +382,9 @@ export async function assertThrowsAsync<T = void>(
|
||||||
try {
|
try {
|
||||||
await fn();
|
await fn();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
if (e instanceof Error === false) {
|
||||||
|
throw new AssertionError("A non-Error object was thrown or rejected.");
|
||||||
|
}
|
||||||
if (ErrorClass && !(Object.getPrototypeOf(e) === ErrorClass.prototype)) {
|
if (ErrorClass && !(Object.getPrototypeOf(e) === ErrorClass.prototype)) {
|
||||||
msg = `Expected error to be instance of "${ErrorClass.name}", but got "${
|
msg = `Expected error to be instance of "${ErrorClass.name}", but got "${
|
||||||
e.name
|
e.name
|
||||||
|
|
|
@ -409,3 +409,86 @@ Deno.test({
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test("Assert Throws Non-Error Fail", () => {
|
||||||
|
assertThrows(
|
||||||
|
() => {
|
||||||
|
assertThrows(
|
||||||
|
() => {
|
||||||
|
throw "Panic!";
|
||||||
|
},
|
||||||
|
String,
|
||||||
|
"Panic!"
|
||||||
|
);
|
||||||
|
},
|
||||||
|
AssertionError,
|
||||||
|
"A non-Error object was thrown."
|
||||||
|
);
|
||||||
|
|
||||||
|
assertThrows(
|
||||||
|
() => {
|
||||||
|
assertThrows(() => {
|
||||||
|
throw null;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
AssertionError,
|
||||||
|
"A non-Error object was thrown."
|
||||||
|
);
|
||||||
|
|
||||||
|
assertThrows(
|
||||||
|
() => {
|
||||||
|
assertThrows(() => {
|
||||||
|
throw undefined;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
AssertionError,
|
||||||
|
"A non-Error object was thrown."
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("Assert Throws Async Non-Error Fail", () => {
|
||||||
|
assertThrowsAsync(
|
||||||
|
() => {
|
||||||
|
return assertThrowsAsync(
|
||||||
|
() => {
|
||||||
|
return Promise.reject("Panic!");
|
||||||
|
},
|
||||||
|
String,
|
||||||
|
"Panic!"
|
||||||
|
);
|
||||||
|
},
|
||||||
|
AssertionError,
|
||||||
|
"A non-Error object was thrown or rejected."
|
||||||
|
);
|
||||||
|
|
||||||
|
assertThrowsAsync(
|
||||||
|
() => {
|
||||||
|
return assertThrowsAsync(() => {
|
||||||
|
return Promise.reject(null);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
AssertionError,
|
||||||
|
"A non-Error object was thrown or rejected."
|
||||||
|
);
|
||||||
|
|
||||||
|
assertThrowsAsync(
|
||||||
|
() => {
|
||||||
|
return assertThrowsAsync(() => {
|
||||||
|
return Promise.reject(undefined);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
AssertionError,
|
||||||
|
"A non-Error object was thrown or rejected."
|
||||||
|
);
|
||||||
|
|
||||||
|
assertThrowsAsync(
|
||||||
|
() => {
|
||||||
|
return assertThrowsAsync(() => {
|
||||||
|
throw undefined;
|
||||||
|
return Promise.resolve("Ok!");
|
||||||
|
});
|
||||||
|
},
|
||||||
|
AssertionError,
|
||||||
|
"A non-Error object was thrown or rejected."
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue