10 examples of 'jest expect async to throw' in JavaScript

Every line of 'jest expect async to throw' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
4async function expectThrow(promise) {
5 try {
6 await promise;
7 throw new Error('Did not throw');
8 } catch (e) {
9 assertCertainError(e, 'Exception while processing transaction: revert');
10 }
11}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

58private async noErrorNotToThrowAsync() {
59 await expect.not.toThrow(async () => Promise.resolve(53));
60}
107public async asyncCheckingWhetherNonFunctionDoesNotThrowShouldThrow(actualValue: any) {
108 const EXPECT = Expect(() => {});
109 (EXPECT as any)._actualValue = actualValue;
110
111 await Expect(async () => {
112 await EXPECT.not.toThrowAsync();
113 }).toThrowErrorAsync(TypeError, "toThrowAsync requires value passed in to Expect to be a function.");
114}
133public async asyncExactErrorThrownUnexpectedly() {
134 const errorFunction = async () => { return new Promise((resolve, reject) => {
135 reject(new TypeError("specific error"));
136 });
137 };
138
139 await Expect(errorFunction).not.toThrowErrorAsync(TypeError, "specific error");
140}
15function failTestOnPromiseRejection(reason) {
16 var message;
17 if (reason instanceof Error) {
18 message = reason.stack;
19 if (reason.message && message && message.indexOf(reason.message) < 0) {
20 // PhantomJS has a `stack` that does not contain the actual
21 // exception message.
22 message = Ember.inspect(reason) + "\n" + message;
23 }
24 } else {
25 message = Ember.inspect(reason);
26 }
27 ok(false, message);
28}
32function asyncFail() {
33 return new Promise((resolve, reject) => {
34 setTimeout(reject, 1000);
35 });
36}
38static try(asyncVal) {
39 return asyncVal;
40}
4export function catchAsyncErrors(fnAsync: any, name: types.errorsType) {
5 return async (req: any, res: any, next: any) => {
6 // await ?
7 fnAsync(req, res, next).catch((e: any) => {
8 let nextError;
9
10 if (e instanceof ServerError) {
11 nextError = e;
12 } else {
13 nextError = new ServerError({
14 name: name,
15 originalError: e
16 });
17 }
18
19 next(nextError);
20 });
21 };
22}
75export function expectFailure(
76 promise: Promise<any>,
77 message?: string
78): Promise<any> {
79 return promise.then(
80 (msg: any) => {
81 throw Error('Expected failure did not occur');
82 },
83 (error: Error) => {
84 if (message && error.message.indexOf(message) === -1) {
85 throw Error(`Error "${message}" not in: "${error.message}"`);
86 }
87 }
88 );
89}
73public async asyncErrorNotThrown() {
74 const errorFunction = async () => { return new Promise((resolve, reject) => {
75 setTimeout(resolve, 400);
76 });
77 };
78
79 await Expect(errorFunction).toThrowAsync();
80}

Related snippets