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.
Comments