NISHIO Hirokazu[Translate]
requestAnimationFrame + jest-electron = unstable behavoir
requestAnimationFrame + jest-electron causes unstable behavoir includes crush of elecron.
To replace requestAnimationFrame with setTimeout solves the problem for now.

solution
ts
beforeEach(() => { jest.spyOn(window, "requestAnimationFrame").mockImplementation(callback => { return setTimeout(callback, 0); }); }); afterEach(() => { window.requestAnimationFrame.mockRestore(); });


-----
OK
ts
test("promise", async () => { let _resolve: (arg: unknown) => void; const p = new Promise(res => { _resolve = res; }).then(() => { console.log("resolved"); }); _resolve!(0); await p; });
resolved / Timeout - Async callback was not invoked within the 5000
ts
test("promise", async () => { let _resolve: (arg: unknown) => void; const p = new Promise(res => { _resolve = res; }).then(() => { console.log("resolved"); }); requestAnimationFrame(() => { _resolve!(0); }); await p; });
OK
ts
test("promise", async () => { let _resolve: (arg: unknown) => void; const p = new Promise(res => { _resolve = res; }).then(() => { console.log("resolved"); }); requestAnimationFrame(() => { _resolve!(0); }); await p; console.log("ok"); });
resolved / Timeout
ts
test("promise", async () => { let _resolve: (arg: unknown) => void; const p = new Promise(res => { _resolve = res; }).then(() => { console.log("resolved"); }); requestAnimationFrame(() => { _resolve!(0); }); await p; expect(1).toBe(1); });

Unstable Behavoir: Fails as expected, crush, or Timeout
ts
test("promise", async () => { let _resolve: (arg: unknown) => void; const p = new Promise(res => { _resolve = res; }).then(() => { console.log("resolved"); }); requestAnimationFrame(() => { _resolve!(0); }); await p; expect(1).toBe(0); });
Timeout

change requestAnimationFrame to setTimeout
OK
ts
test("promise", async () => { let _resolve: (arg: unknown) => void; const p = new Promise(res => { _resolve = res; }).then(() => { console.log("resolved"); }); setTimeout(() => { _resolve!(0); }); await p; });
OK
ts
test("promise", async () => { let _resolve: (arg: unknown) => void; const p = new Promise(res => { _resolve = res; }).then(() => { console.log("resolved"); }); setTimeout(() => { _resolve!(0); }); await p; expect(1).toBe(1); });

solution
ts
beforeEach(() => { jest.spyOn(window, "requestAnimationFrame").mockImplementation(callback => { return setTimeout(callback, 0); }); }); afterEach(() => { window.requestAnimationFrame.mockRestore(); });


"Engineer's way of creating knowledge" the English version of my book is now available on [Engineer's way of creating knowledge]

(C)NISHIO Hirokazu / Converted from [Scrapbox] at [Edit]