Skip to content

Commit

Permalink
Allow async functions in ReactNativeTester and add tests (facebook#48063
Browse files Browse the repository at this point in the history
)

Summary:

Changelog: [internal]

This allows async functions to be passed to `runTask` (just a type change really) and adds tests for ReactNativeTester. Error handling isn't currently set up correctly, so those tests are disabled for now.

Reviewed By: sammy-SC

Differential Revision: D66698547
  • Loading branch information
rubennorte authored and facebook-github-bot committed Dec 4, 2024
1 parent 0217d7e commit 5475e6f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @oncall react_native
*/

import '../../../Libraries/Core/InitializeCore';
import * as ReactNativeTester from './ReactNativeTester';

describe('ReactNativeTester', () => {
describe('runTask', () => {
it('should run a task synchronously', () => {
const task = jest.fn();

ReactNativeTester.runTask(task);

expect(task).toHaveBeenCalledTimes(1);
});

// TODO: fix error handling and make this pass
it.skip('should re-throw errors from the task synchronously', () => {
expect(() => {
ReactNativeTester.runTask(() => {
throw new Error('test error');
});
}).toThrow('test error');
});

it('should exhaust the microtask queue synchronously', () => {
const lastMicrotask = jest.fn();

ReactNativeTester.runTask(() => {
queueMicrotask(() => {
queueMicrotask(() => {
queueMicrotask(() => {
queueMicrotask(lastMicrotask);
});
});
});
});

expect(lastMicrotask).toHaveBeenCalledTimes(1);
});

// TODO: fix error handling and make this pass
it.skip('should re-throw errors from microtasks synchronously', () => {
expect(() => {
ReactNativeTester.runTask(() => {
queueMicrotask(() => {
throw new Error('test error');
});
});
}).toThrow('test error');
});

it('should run async tasks synchronously', () => {
let completed = false;

ReactNativeTester.runTask(async () => {
await Promise.resolve(6);
completed = true;
});

expect(completed).toBe(true);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Root {
*
* React must run inside of event loop to ensure scheduling environment is closer to production.
*/
export function runTask(task: () => void) {
export function runTask(task: () => void | Promise<void>) {
nativeRuntimeScheduler.unstable_scheduleCallback(
schedulerPriorityImmediate,
task,
Expand Down

0 comments on commit 5475e6f

Please sign in to comment.