From feead8f2995b9ba9940919ce93ec2d9640ae8495 Mon Sep 17 00:00:00 2001 From: Rob Hogan Date: Mon, 12 Sep 2022 03:57:45 -0700 Subject: [PATCH] Allow LogBoxLog-test to work with native promises Summary: This test currently uses `jest.runAllTicks()` to execute cached, immediately-resolving promises, under Jest "legacy" timers (the RN default) - this works only because [we polyfill Promise](https://github.com/facebook/react-native/blob/main/jest/setup.js#L24) using a userland JavaScript implementation that internally uses mocked-out timer functions. Here we change to a more universal approach by adding a new microtask/promise to the end of the queue and awaiting it. This allows us to remove our Promise polyfill from Jest setup (to follow). Changelog: [Internal][Fixed] - Prepare `LogBoxLog-test.js` for native promises Reviewed By: huntie Differential Revision: D39418413 fbshipit-source-id: 1384ef385b1e10261754513369af8997d296ffea --- .../LogBox/Data/__tests__/LogBoxLog-test.js | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/Libraries/LogBox/Data/__tests__/LogBoxLog-test.js b/Libraries/LogBox/Data/__tests__/LogBoxLog-test.js index bd26449ba0a96d..98b758d9b62dca 100644 --- a/Libraries/LogBox/Data/__tests__/LogBoxLog-test.js +++ b/Libraries/LogBox/Data/__tests__/LogBoxLog-test.js @@ -57,6 +57,10 @@ const createStack = (methodNames: Array) => methodName, })); +// Adds a new task to the end of the microtask queue, so that awaiting this +// function will run all queued immediates +const runMicrotasks = async () => {}; + describe('LogBoxLog', () => { beforeEach(() => { jest.resetModules(); @@ -132,7 +136,7 @@ describe('LogBoxLog', () => { expect(getLogBoxSymbolication().symbolicate).not.toBeCalled(); }); - it('updates when symbolication finishes', () => { + it('updates when symbolication finishes', async () => { const log = getLogBoxLog(); const callback = jest.fn(); @@ -141,7 +145,7 @@ describe('LogBoxLog', () => { expect(callback).toBeCalledWith('PENDING'); expect(getLogBoxSymbolication().symbolicate).toBeCalled(); - jest.runAllTicks(); + await runMicrotasks(); expect(callback).toBeCalledTimes(2); expect(callback).toBeCalledWith('COMPLETE'); @@ -156,13 +160,14 @@ describe('LogBoxLog', () => { getLogBoxSymbolication().symbolicate.mockClear(); log.symbolicate(callback); - jest.runAllTicks(); + + await runMicrotasks(); expect(callback).toBeCalledTimes(0); expect(getLogBoxSymbolication().symbolicate).not.toBeCalled(); }); - it('updates when symbolication fails', () => { + it('updates when symbolication fails', async () => { const error = new Error('...'); getLogBoxSymbolication().symbolicate.mockImplementation(async stack => { throw error; @@ -176,7 +181,7 @@ describe('LogBoxLog', () => { expect(callback).toBeCalledWith('PENDING'); expect(getLogBoxSymbolication().symbolicate).toBeCalled(); - jest.runAllTicks(); + await runMicrotasks(); expect(callback).toBeCalledTimes(2); expect(callback).toBeCalledWith('FAILED'); @@ -191,7 +196,8 @@ describe('LogBoxLog', () => { getLogBoxSymbolication().symbolicate.mockClear(); log.symbolicate(callback); - jest.runAllTicks(); + + await runMicrotasks(); expect(callback).toBeCalledTimes(0); expect(getLogBoxSymbolication().symbolicate).not.toBeCalled(); @@ -221,7 +227,7 @@ describe('LogBoxLog', () => { expect(getLogBoxSymbolication().symbolicate).not.toBeCalled(); }); - it('retry updates when symbolication finishes', () => { + it('retry updates when symbolication finishes', async () => { const log = getLogBoxLog(); const callback = jest.fn(); @@ -230,7 +236,7 @@ describe('LogBoxLog', () => { expect(callback).toBeCalledWith('PENDING'); expect(getLogBoxSymbolication().symbolicate).toBeCalled(); - jest.runAllTicks(); + await runMicrotasks(); expect(callback).toBeCalledTimes(2); expect(callback).toBeCalledWith('COMPLETE'); @@ -251,7 +257,7 @@ describe('LogBoxLog', () => { expect(getLogBoxSymbolication().symbolicate).not.toBeCalled(); }); - it('retry updates when symbolication fails', () => { + it('retry updates when symbolication fails', async () => { const error = new Error('...'); getLogBoxSymbolication().symbolicate.mockImplementation(async stack => { throw error; @@ -265,7 +271,7 @@ describe('LogBoxLog', () => { expect(callback).toBeCalledWith('PENDING'); expect(getLogBoxSymbolication().symbolicate).toBeCalled(); - jest.runAllTicks(); + await runMicrotasks(); expect(callback).toBeCalledTimes(2); expect(callback).toBeCalledWith('FAILED'); @@ -289,7 +295,7 @@ describe('LogBoxLog', () => { expect(callback).toBeCalledWith('PENDING'); expect(getLogBoxSymbolication().symbolicate).toBeCalled(); - jest.runAllTicks(); + await runMicrotasks(); expect(callback).toBeCalledTimes(2); expect(callback).toBeCalledWith('COMPLETE');