Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop polyfilling Promise in Jest tests #34659

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 6 additions & 2 deletions Libraries/LogBox/Data/__tests__/LogBoxSymbolication-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
'use strict';

import type {StackFrame} from '../../../Core/NativeExceptionsManager';
import type {SymbolicatedStackTrace} from '../../../Core/Devtools/symbolicateStackTrace';

jest.mock('../../../Core/Devtools/symbolicateStackTrace');

const LogBoxSymbolication = require('../LogBoxSymbolication');

const symbolicateStackTrace: JestMockFn<
$ReadOnlyArray<Array<StackFrame>>,
Promise<Array<StackFrame>>,
Promise<SymbolicatedStackTrace>,
> = (require('../../../Core/Devtools/symbolicateStackTrace'): any);

const createStack = (methodNames: Array<string>) =>
Expand All @@ -33,7 +34,10 @@ const createStack = (methodNames: Array<string>) =>
describe('LogBoxSymbolication', () => {
beforeEach(() => {
jest.resetModules();
symbolicateStackTrace.mockImplementation(async stack => stack);
symbolicateStackTrace.mockImplementation(async stack => ({
stack,
codeFrame: null,
}));
});

it('symbolicates different stacks', () => {
Expand Down
1 change: 0 additions & 1 deletion jest/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ global.performance = {
now: jest.fn(Date.now),
};

global.Promise = jest.requireActual('promise');
global.regeneratorRuntime = jest.requireActual('regenerator-runtime/runtime');
global.window = global;

Expand Down