introduce ability to flush messege queue from tests (#47646)
Some checks are pending
Label closed PR as merged and leave a comment / comment-and-label (push) Waiting to run
Publish Bumped Packages / publish_bumped_packages (push) Waiting to run
Update node modules cache / update_node_modules_cache (push) Waiting to run

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/47646

changelog: [internal]

In order to control scheduling of tests, test setup must be separated from test running. This is because of how internal scheduling of React Native is restricted until bundle is loaded via BufferedRuntimeExecutor. This is the reason for executing tests by calling global function `$$RunTests$$`. This is not a pretty solution but it is hidden within testing infrastructure and users of our test infra are not exposed to this.

There is a new method exposed to JavaScript: flushMessageQueue. This will flush everything that is inside of message queue. This is the queue where tasks are queued whenever RuntimeScheduler enqueues something by calling `runtimeExecutor_`.

Reviewed By: christophpurrer

Differential Revision: D65951894

fbshipit-source-id: 2e8e0c10fbeb998f4a51ee6d01ef229eb5f70448
This commit is contained in:
Samuel Susla 2024-11-17 03:30:31 -08:00 committed by Facebook GitHub Bot
parent 6f1c2a512e
commit 89a7238acd
2 changed files with 4 additions and 47 deletions

View File

@ -248,9 +248,12 @@ function reportTestSuiteResult(testSuiteResult: TestSuiteResult): void {
console.log(JSON.stringify(testSuiteResult));
}
global.$$RunTests$$ = () => {
executeTests();
};
export function registerTest(setUpTest: () => void) {
runWithGuard(() => {
setUpTest();
executeTests();
});
}

View File

@ -1,46 +0,0 @@
/**
* 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
*/
import type {MixedElement} from 'react';
import ReactFabric from '../../../Libraries/Renderer/shims/ReactFabric';
let globalSurfaceIdCounter = 1;
class Root {
#surfaceId: number;
#hasRendered: boolean = false;
constructor() {
this.#surfaceId = globalSurfaceIdCounter;
globalSurfaceIdCounter += 10;
}
render(element: MixedElement) {
if (!this.#hasRendered) {
global.$$JSTesterModuleName$$.startSurface(this.#surfaceId);
this.#hasRendered = true;
}
ReactFabric.render(element, this.#surfaceId);
}
destroy() {
// TODO: check for leaks.
global.$$JSTesterModuleName$$.stopSurface(this.#surfaceId);
}
// TODO: add an API to check if all surfaces were deallocated when tests are finished.
}
// TODO: Add option to define surface props and pass it to startSurface
// Surfacep rops: concurrentRoot, surfaceWidth, surfaceHeight, layoutDirection, pointScaleFactor.
export function createRoot(): Root {
return new Root();
}