Skip to content

Commit

Permalink
Allow LogBoxLog-test to work with native promises
Browse files Browse the repository at this point in the history
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
  • Loading branch information
robhogan authored and facebook-github-bot committed Sep 12, 2022
1 parent 8e0168f commit feead8f
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions Libraries/LogBox/Data/__tests__/LogBoxLog-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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');
Expand All @@ -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;
Expand All @@ -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');
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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');
Expand All @@ -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;
Expand All @@ -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');
Expand All @@ -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');
Expand Down

0 comments on commit feead8f

Please sign in to comment.