From 74595907a2b70a600522a8be3d4e3f48f0eb414c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Tue, 5 Mar 2024 04:10:44 -0800 Subject: [PATCH] Add JSI test for queueMicrotask and drainMicrotasks (#43309) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/43309 Changelog: [internal] X-link: https://github.com/facebook/hermes/pull/1336 Add JSI test verifying the behavior of `queueMicrotask` and `drainMicrotasks` in the runtimes that support them. Reviewed By: neildhar Differential Revision: D54484771 fbshipit-source-id: e8c0c8e05215d59a0a8c86161452642c41bcdbd7 --- .../ReactCommon/jsi/jsi/test/testlib.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/packages/react-native/ReactCommon/jsi/jsi/test/testlib.cpp b/packages/react-native/ReactCommon/jsi/jsi/test/testlib.cpp index 8291d428b35..d9090bdb2aa 100644 --- a/packages/react-native/ReactCommon/jsi/jsi/test/testlib.cpp +++ b/packages/react-native/ReactCommon/jsi/jsi/test/testlib.cpp @@ -1358,6 +1358,38 @@ TEST_P(JSITest, JSErrorTest) { JSIException); } +TEST_P(JSITest, MicrotasksTest) { + try { + rt.global().setProperty(rt, "globalValue", String::createFromAscii(rt, "")); + + auto microtask1 = + function("function microtask1() { globalValue += 'hello'; }"); + auto microtask2 = + function("function microtask2() { globalValue += ' world' }"); + + rt.queueMicrotask(microtask1); + rt.queueMicrotask(microtask2); + + EXPECT_EQ( + rt.global().getProperty(rt, "globalValue").asString(rt).utf8(rt), ""); + + rt.drainMicrotasks(); + + EXPECT_EQ( + rt.global().getProperty(rt, "globalValue").asString(rt).utf8(rt), + "hello world"); + + // Microtasks shouldn't run again + rt.drainMicrotasks(); + + EXPECT_EQ( + rt.global().getProperty(rt, "globalValue").asString(rt).utf8(rt), + "hello world"); + } catch (const JSINativeException& ex) { + // queueMicrotask() is unimplemented by some runtimes, ignore such failures. + } +} + //---------------------------------------------------------------------- // Test that multiple levels of delegation in DecoratedHostObjects works.