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.