({fn, timeout, initError = new Error()}: QueueableFn)
| 41 | |
| 42 | // eslint-disable-next-line unicorn/error-message |
| 43 | const mapper = ({fn, timeout, initError = new Error()}: QueueableFn) => { |
| 44 | let promise = new Promise<void>(resolve => { |
| 45 | const next = function (...args: [Error]) { |
| 46 | const err = args[0]; |
| 47 | if (err) { |
| 48 | options.fail.apply(null, args); |
| 49 | } |
| 50 | resolve(); |
| 51 | }; |
| 52 | |
| 53 | next.fail = function (...args: [Error]) { |
| 54 | options.fail.apply(null, args); |
| 55 | resolve(); |
| 56 | }; |
| 57 | try { |
| 58 | fn.call(options.userContext, next); |
| 59 | } catch (error: any) { |
| 60 | options.onException(error); |
| 61 | resolve(); |
| 62 | } |
| 63 | }); |
| 64 | |
| 65 | promise = Promise.race<void>([promise, token]); |
| 66 | |
| 67 | if (!timeout) { |
| 68 | return promise; |
| 69 | } |
| 70 | |
| 71 | const timeoutMs: number = timeout(); |
| 72 | |
| 73 | return pTimeout( |
| 74 | promise, |
| 75 | timeoutMs, |
| 76 | options.clearTimeout, |
| 77 | options.setTimeout, |
| 78 | () => { |
| 79 | initError.message = `Timeout - Async callback was not invoked within the ${formatTime( |
| 80 | timeoutMs, |
| 81 | )} timeout specified by jest.setTimeout.`; |
| 82 | initError.stack = initError.message + initError.stack; |
| 83 | options.onException(initError); |
| 84 | }, |
| 85 | ); |
| 86 | }; |
| 87 | |
| 88 | const result = options.queueableFns.reduce( |
| 89 | (promise, fn) => promise.then(() => mapper(fn)), |
no test coverage detected