Skip to content

Commit

Permalink
Allow async functions in ReactNativeTester and add tests
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.

Differential Revision: D66698547
  • Loading branch information
rubennorte authored and facebook-github-bot committed Dec 3, 2024
1 parent 67bff87 commit b037250
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', () => {

Check warning on line 26 in packages/react-native/src/private/__tests__/ReactNativeTester-itest.js

View workflow job for this annotation

GitHub Actions / test_js (20)

Disabled test

Check warning on line 26 in packages/react-native/src/private/__tests__/ReactNativeTester-itest.js

View workflow job for this annotation

GitHub Actions / test_js (18)

Disabled test
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', () => {

Check warning on line 51 in packages/react-native/src/private/__tests__/ReactNativeTester-itest.js

View workflow job for this annotation

GitHub Actions / test_js (20)

Disabled test

Check warning on line 51 in packages/react-native/src/private/__tests__/ReactNativeTester-itest.js

View workflow job for this annotation

GitHub Actions / test_js (18)

Disabled test
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 b037250

Please sign in to comment.