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

Allow async functions in ReactNativeTester and add tests #48063

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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
Loading