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”
This talk has been presented at React Summit US 2023, check out the latest edition of this React Conference.
FAQ
The Atlas Device SDK, formerly known as Realm, is a toolkit that features a local database running in mobile apps. It supports bidirectional synchronization of data between the app and a cloud database, and operates with very low latency, even without constant internet connectivity.
The Atlas Device SDK supports multiple programming languages including TypeScript, Swift, Kotlin, and .NET. It utilizes a common core component written in C++ for cross-platform compatibility.
Mocha Remote is used to facilitate testing across various platforms by running tests from a CLI interface on a host machine, allowing tests to execute on a device and report results back to the host. This setup improves developer experience by maintaining consistent test environments and reducing the need for environment-specific adjustments.
The Atlas Device SDK allows for bidirectional data synchronization between a local app and a cloud database. It requires internet connectivity only when it needs to synchronize data, otherwise it can operate offline, ensuring data is always available with minimal delay.
Mocha Remote and Jest Remote serve similar purposes but are built around different testing frameworks. Mocha Remote is based on the Mocha testing framework, whereas Jest Remote is built around Jest, which is commonly used in React Native projects. Jest Remote is notable for its pluggable runner, which is configurable from the CLI.
Jest Remote aims to support running tests in separate Node processes and plans to extend this functionality to other platforms supported by the Atlas Device SDK, enhancing versatility for developers working in different environments.
The Atlas Device SDK enhances mobile app development by providing a local database for low-latency data access, supporting robust offline functionality, and enabling seamless data synchronization with cloud services. This toolkit helps developers create more reactive and resilient mobile applications.
The Mocha Remote CLI streamlines testing by allowing developers to run tests across multiple platforms and devices directly from their terminal. It utilizes a WebSocket connection for real-time feedback and integrates seamlessly with existing development workflows, thereby enhancing efficiency and ease of use.
1. Introduction to Atlas Device SDKs#
I work for MongoDB on the Atlas Device SDKs, supporting multiple operating systems, platforms, and JavaScript engines. Today, I want to share a technique we use to test our code anywhere it needs to run. MongoDB Atlas is a multicloud developer data platform, enabling you to build with data. We built the Atlas Device SDKs, which features a local database running in the app and bidirectional synchronization of data between the app and the cloud. We have support for multiple programming languages and leverage code generation to expose a common core component. We want to test across all supported platforms and have an awesome developer experience.
Hi there. I'm Krane. I work for MongoDB on what we used to call Rel, but is now known as the Atlas Device SDKs. I help build and maintain our TypeScript package, supporting multiple operating systems, platforms, and JavaScript engines.
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.
And for that to happen, we have a common core component written in C++. And our team leverage code generation to expose this core component as a TypeScript interface on which we build the SDK on top of. And for that to happen, we generate code, glue code, binding code, we call it, between this core component and the various JavaScript engines and their C++ APIs. We currently support JavaScript core at Hermes via JSI for React Native and we also support V8 via NAPI for Node.js and Electron. We also have a preview of Wasm support for the browsers. So we have all this handcrafted TypeScript code and rules that we use to generate code for all these different platforms with JavaScript engines that behave slightly differently.
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.
Check out more articles and videos
We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career
Workshops on related topic
In this three-hour workshop we’ll introduce React Testing Library along with a mental model for how to think about designing your component tests. This mental model will help you see how to test each bit of logic, whether or not to mock dependencies, and will help improve the design of your components. You’ll walk away with the tools, techniques, and principles you need to implement low-cost, high-value component tests.
Table of contents- The different kinds of React application tests, and where component tests fit in- A mental model for thinking about the inputs and outputs of the components you test- Options for selecting DOM elements to verify and interact with them- The value of mocks and why they shouldn’t be avoided- The challenges with asynchrony in RTL tests and how to handle them
Prerequisites- Familiarity with building applications with React- Basic experience writing automated tests with Jest or another unit testing framework- You do not need any experience with React Testing Library- Machine setup: Node LTS, Yarn
Tests rely on many conditions and are considered to be slow and flaky. On the other hand - end-to-end tests can give the greatest confidence that your app is working. And if done right - can become an amazing tool for boosting developer velocity.
Detox is a gray-box end-to-end testing framework for mobile apps. Developed by Wix to solve the problem of slowness and flakiness and used by React Native itself as its E2E testing tool.
Join me on this workshop to learn how to make your mobile end-to-end tests with Detox rock.
Prerequisites- iOS/Android: MacOS Catalina or newer- Android only: Linux- Install before the workshop
1. Welcome to Postman- Explaining the Postman User Interface (UI)2. Workspace and Collections Collaboration- Understanding Workspaces and their role in collaboration- Exploring the concept of Collections for organizing and executing API requests3. Introduction to API Testing- Covering the basics of API testing and its significance4. Variable Management- Managing environment, global, and collection variables- Utilizing scripting snippets for dynamic data5. Building Testing Workflows- Creating effective testing workflows for comprehensive testing- Utilizing the Collection Runner for test execution- Introduction to Postbot for automated testing6. Advanced Testing- Contract Testing for ensuring API contracts- Using Mock Servers for effective testing- Maximizing productivity with Collection/Workspace templates- Integration Testing and Regression Testing strategies7. Automation with Postman- Leveraging the Postman CLI for automation- Scheduled Runs for regular testing- Integrating Postman into CI/CD pipelines8. Performance Testing- Demonstrating performance testing capabilities (showing the desktop client)- Synchronizing tests with VS Code for streamlined development9. Exploring Advanced Features - Working with Multiple Protocols: GraphQL, gRPC, and more
Join us for this workshop to unlock the full potential of Postman for API testing, streamline your testing processes, and enhance the quality and reliability of your software. Whether you're a beginner or an experienced tester, this workshop will equip you with the skills needed to excel in API testing with Postman.
Workshop level: Intermediate
Comments