Home

Awesome

Mocha Test Explorer remoting utility functions

This package contains utility functions for running tests remotely with Mocha Test Explorer.

Running tests remotely

You can let the Mocha Test Explorer run your tests in a remote environment (e.g. in a docker container or via ssh on a remote machine) by writing a launcher script. Usually, when Mocha Test Explorer loads or runs your tests, it does so by running a worker script in a child process and communicating with it using the mocha worker protocol (which is a simple internal protocol of Mocha Test Explorer). If you write a launcher script and set mochaExplorer.launcherScript in your VS Code settings to the location of that script, Mocha Test Explorer will run your launcher script instead of its worker script. Your launcher script should then run the worker script in the remote environment and forward the worker protocol messages between Mocha Test Explorer and the remotely running worker script.

There are example projects containing well-documented launcher scripts for running your tests in a docker container or on another machine via ssh.

Details of the mocha worker protocol

The mocha worker protocol is a very simple message-based protocol that is closely related to the Test Adapter API. It is usually sent via a node IPC channel (using process.send(...) and process.on('message', ...)) or a TCP/IP connection.

The first message of the protocol is a WorkerArgs object sent by Mocha Test Explorer. It contains all the necessary information about what the worker script should do.

All subsequent messages are sent by the worker script. They contain the worker's results:

The launcher script

In order to load or run the tests in a remote environment, the launcher script needs to:

(The last 2 points are optional but highly recommended to help with troubleshooting)

Communicating with the worker

The worker script usually communicates with Mocha Test Explorer using a node IPC channel, but this channel is not available when running the worker remotely, so we need to use a TCP/IP connection instead. To tell the worker to use TCP/IP, the launcher script must pass a JSON-encoded NetworkOptions object as the first (command-line) argument to the worker process. The NetworkOptions tell the worker if it should act as a TCP server or client (the role property), which port it should use and (optionally) the host it should connect to (when acting as a TCP client) or which host address it should listen on (when acting as a TCP server).

The launcher script can use the createConnection() (if the worker acts as a TCP server) or receiveConnection (if the worker acts as a TCP client) functions from this package to establish the connection to the worker.

The worker protocol messages are JSON-encoded and separated by a newline character. This package also contains the writeMessage() and readMessages() functions that take care of this encoding.