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
This commit is contained in:
Rob Hogan
2022-09-12 03:57:45 -07:00
committed by Facebook GitHub Bot
parent 8e0168fe9d
commit feead8f299
@@ -57,6 +57,10 @@ const createStack = (methodNames: Array<string>) =>
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');