Running tests on Node.js is common practice for app and library developers. But when your library is integrating more deeply with the platform, operating system or JS engine, you need to run tests “on device” increases. The Realm JS team at MongoDB is faced with this challenge, as we’re maintaining a React Native library with native modules. In this talk I’ll share the “mocha-remote” package which we’ve built to help us run tests “on device” while getting reporting and remaining in control from the comfort of our terminals.
How Realm by MongoDB is Testing Native Modules “on device”
AI Generated Video Summary
The speaker works on the Atlas Device SDKs, which allows testing code across multiple platforms and has support for multiple programming languages. They also built Mocha Remote CLI, a tool for running tests on Node.js and in a browser. The speaker mentions the popularity of Jest and the alternative Vitest for running tests on platforms like Android and iOS.
1. Introduction to Atlas Device SDKs
And today, I want to share a technique we use to help test our code anywhere it needs to run. MongoDB Atlas is a multicloud developer data platform, an integrated suite of cloud database and data services, enabling you to build with data. And one part of this is about bringing data closer to where it's being used. And we call this Atlas for the Edge.
One place where your data is being used is in your end users mobile devices. And for that, we built the Atlas Device SDKs, formally known as Realm. So the Atlas device SDKs features a local database running in the app. It has bidirectional synchronization of data between the app and the database running in the cloud. And it doesn't require internet connectivity, unless it needs to synchronize, of course. And because it doesn't have this requirement, when you read and write data, it's very low latency. And besides those APIs, we also have APIs to observe data as they change on the server and propagates to the device. And this enables you to build truly reactive user experiences. Besides TypeScript, we have support for Swift, Kotlin, .NET, just to name a few.
So we want to naturally test this across all the supported platforms before releasing something into the apps of thousands of users. When I joined, we had a single package exporting multiple async functions and we had specific test environments that were handcrafted code, some of it native, that wasn't tested at all. It was a nightmare to maintain, especially as we wanted to progress and update these environments to the newest versions of the of the platforms we supported. And this lack of developer experience grew on me. I just wanted to be able to run the test in watch mode on any platform that we supported, have an awesome developer experience, instead of spending my time upgrading these platform runners or platform-specific running code. And I thought to myself, what if we just ran the same code across all the platforms, and we drove it from the comfort of our terminals, and we could write and maintain, and more importantly, test this code separately from the environments in which we tested. I essentially just wanted to run a CLI on my host machine, execute the tests on device, and get the reporting back on my host machine.
2. Building Mocha Remote CLI
In 2019, I built Mocha Remote, a tool based on Mocha for running tests on Node.js and in a browser. It supports almost all options of Mocha, including timeouts, specifying reporters, and a context. The Mocha runner was bundled into a runtime independent variant using webpack, and a WebSocket connection is used between the runner, client, and Mocha Remote CLI server. The CLI can grep for specific tests and supply a context. The Mocha Remote CLI is linked to a sub-command responsible for starting the test app, ensuring correct status code propagation.
So in 2019, that's exactly what I built. I built this around Mocha. It honestly probably didn't do too much investigation. It looked popular, and I knew that it could run tests both on Node.js and in a browser. And it was used elsewhere in our organization.
And the Mocha CLI, basically, this is a slide about the simplified architecture of Mocha. Basically, once you execute the CLI, it instantiates a runner, which requires in the test files, and then it starts execution. And the progress of passing and failing tests are emitted through events back to the CLI, which is then relayed to the reporters. And I named my tool Mocha Remote based on this. It's not as popular as Mocha as you can see, but and mostly it's 900 weekly downloads. Most of them are probably our CI. And this is part of why I want to share this story today.
I tried reusing the Mocha CLI, but I found it difficult to extend because the runner itself is not extendable or it's not possible to configure from the CLI of Mocha, but fortunately it was very easy to implement, re-implement that, and the Mocha Remote CLI now supports almost all of the options that you can pass to Mocha, including grabbing for a test title, including timeouts, specifying reporters, watching, and then also a context, which I will show later.
So the Mocha runner, the original Mocha runner, I was actually able to bundle that into a runtime independent variant using webpack, and this is what runs on the various platforms. Instead of just simple events, using event emitting, we use a WebSocket connection between the runner, client, and the Mocha Remote CLI server. And once the client connects to the server, it will get a message to start running tests. The client or the runner will execute a test function supplied by the developer, which will basically define the tests either by importing them or just defining them inline. And once the test runner starts running, it will start sending these passing and failing tests events over a web socket back to the CLI, which is propagated to the reporters.
This is what it looks like when you invoke the Mocha Remote CLI, and you can see we grep for all the tests that has connects in the title, and we also supply this context, in case an API URL, which is propagated through this system into the runner, and I'll show this in a second. We also supply the sub-command noderuntestappjs, which is responsible for starting up the test app. And the lifetime of this sub-command and the Mocha Remote CLI is linked together in a way that if the sub-command exits prematurely, the Mocha Remote CLI will also exit. And by and vice versa. So it also ensures that the correct status code is propagated when the tests are failing in the device or the device crashes or whatever, the Mocha Remote CLI will get the correct non-zero exit code.
Cool. I believe this is almost time for demos. I just want to show here, this is what it looks like in a client. It gets the test, or you supply the test function and it gets a context path, this argument, and you can see how this context is able to be used when you instantiate this MyApp instance. Cool. I want to show first running, invoking the Mocha Remote CLI here on node.js. And this starts up a separate node process that executes the tests.
3. Running Tests on Android, iOS, and Other Platforms
We can also do it for Android, iOS, and other platforms. Maka was popular in 2016, but Jest is now the de facto standard in React Native projects. I've started the Jest remote package to support running tests in a separate Node process. Another alternative, Vitest, is gaining attention from the React web community. It has an experimental browser mode and a platform-independent runner.
We can also do it for Android here. It will basically invoke the Metro, the metro bundle as one part of it The other part of the sub-command will execute the React Native CLI, and you can see the emulator will start up and starts requiring in the bundle and execute the tests. And thankfully it passed, which is great. I don't want to spend time, but we can also do it for iOS.
Yeah. Let's go back to the slides. Good. So, Maka was popular in 2016 when we started the Realm.js Project. Arguably it's still popular with 7 million weekly downloads, but not as popular as Jest. And Jest is the de facto standard in React Native projects. And this is why I've also started the Jest remote package. It's built around the same architecture and it's a good match because the Jest runner is actually pluggable from the CLI. So it's possible to specify this Jest remote runner. And currently, in its current form, as I'm recording this, it supports running the tests in a separate Node process. But my vision is to, of course, support all the other platforms that we also support with Maka Remote.
Another competitor, or you can say an alternative to Jest, which is catching a lot of attention from the React web community especially, is Vitest. And from my initial investigations and conversations with maintainers, it looks like this will be an even better match as it also has experimental browser mode. So the runner is already built in a way that's platform and runtime independent.
Thank you so much for your time and attention. This is a rare commodity these days. And I hope that you found this useful, especially if you too as a maintainer of a package like this. And if you have any questions, feel free to reach out. All my social media handles are in my Git Nation profile. Also, please help me build this for Vitest or Jest if you want to, if you have time and thinks it's interesting. I wish you the best of luck with all of your endeavors.