JsTestDriver is an open-source, Java-based test runner designed by Google to make JavaScript unit testing seamless, powerful, and highly integrated into continuous integration (CI) environments. It functions by launching a server, capturing multiple browsers as “slaves,” and pushing code directly to them via the command line to execute tests instantly in real browser environments.
Note: While historical documentation and plugin ecosystems like the JetBrains JSTestDriver Plugin exist, modern web development generally favors Jest or Karma for new projects. Core Architecture JsTestDriver relies on a client-server relationship:
The Server: A lightweight Java process (.jar) that manages communication.
Slave Browsers: Actual browsers (Chrome, Firefox, Safari) opened to a specific URL that sit waiting for instructions.
The Runner: A command-line client that tells the server to load the source files, run the test suites, and stream results back. 1. Setting Up the Environment
To get started, you need Java Runtime Environment (JRE) installed on your machine. Download the JsTestDriver.jar file from the repository. Create a basic project directory structure:
my-project/ ├── src/ │ └── greeter.js ├── src-test/ │ └── greeter_test.js ├── jsTestDriver.conf └── JsTestDriver.jar Use code with caution. 2. Creating the Configuration File
JsTestDriver uses a YAML-formatted configuration file (traditionally named jsTestDriver.conf) to determine where the server lives and what files to inject.
server: http://localhost:9876 load: - src/.js - src-test/.js Use code with caution. server: Points to the target runner server location.
load: Tells the runner which source and test files to serve up, using standard glob patterns. 3. Writing Your First Test
JsTestDriver includes its own built-in TestCase structure resembling classical JUnit testing patterns. Source Code (src/greeter.js): javascript function greet(name) { return “Hello, ” + name + “!”; } Use code with caution. Test Code (src-test/greeter_test.js): javascript
GreeterTest = TestCase(“GreeterTest”); GreeterTest.prototype.testGreeting = function() { var result = greet(“World”); assertEquals(“Hello, World!”, result); }; Use code with caution. 4. Running the Test Suite
Execution requires launching the server, linking the browsers, and executing the command runner. Step 1: Start the Server
Run this terminal command from your root directory to spin up the runner on port 9876: java -jar JsTestDriver.jar –port 9876 Use code with caution. Step 2: Capture Slave Browsers
Open any local browser window and navigate to http://localhost:9876. Click the “Capture This Browser” link on the landing page. The browser is now an active automated test worker. Step 3: Execute Tests
Open a second terminal window and tell the runner jar to trigger execution against your active config: java -jar JsTestDriver.jar –tests all Use code with caution.
The command line will quickly display whether your tests passed or failed across every captured browser simultaneously. Key Capabilities for Advanced Workflows
Code Coverage for JavaScript Unit Testing | The WebStorm Blog
Leave a Reply