MCPcopy
hub / github.com/jestjs/jest / queueRunner

Function queueRunner

packages/jest-jasmine2/src/queueRunner.ts:34–99  ·  view source on GitHub ↗
(options: Options)

Source from the content-addressed store, hash-verified

32type PromiseCallback = (() => void | PromiseLike<void>) | undefined | null;
33
34export default function queueRunner(options: Options): PromiseLike<void> & {
35 cancel: () => void;
36 catch: (onRejected?: PromiseCallback) => Promise<void>;
37} {
38 const token = new PCancelable<void>((onCancel, resolve) => {
39 onCancel(resolve);
40 });
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)),
90 Promise.resolve(),
91 );

Callers 2

queueRunnerFactoryMethod · 0.85

Calls 3

mapperFunction · 0.85
thenMethod · 0.80
resolveMethod · 0.45

Tested by

no test coverage detected