Testing JavaScript with Cucumber.js
JavaScript unit tests are run in a browser against a test server which actually handles the testing process.
This server allows you to capture a browser to run tests in, loads the test targets to the captured browser, controls the testing process, and exchanges data between the browser and WebStorm, so you can view test results without leaving the IDE.
The test server does not necessarily have to be on your machine, it can be launched right from WebStorm through the Cucumber.js test runner. Cucumber.js runs tests that are called features and are written in the Gherkin language. Each feature is described in a separate file with the extension feature
.
On this page:
Before you start
- Download and install Node.js. The runtime environment is required for two reasons:
- The Cucumber.js test runner is started through Node.js.
- NPM, which is a part of the runtime environment, is also the easiest way to download the Cucumber.js test runner.
If you are going to use the command line mode, make sure the path to the parent folder of the Node.js executable file and the path to the
npm
folder are added to thePATH
variable. This enables you to launch the Cucumber.js test runner and npm from any folder. - Make sure the Node.js plugin is enabled. The plugin is bundled with WebStorm and activated by default. If the plugin is not activated, enable it on the Plugins page of the Settings / Preferences Dialog as described in Enabling and Disabling Plugins.
- Make sure the Cucumber.js and Gherkin plugins are enabled. The plugins are bundled with WebStorm and activated by default. If the plugins are not activated, enable them on the Plugins page of the Settings / Preferences Dialog as described in Enabling and Disabling Plugins.
Installing Cucumber.js
You can install Cucumber.js in the current project (locally), or globally, or as a development dependency.
- Local installation in a specific project restricts the use of the test runner to this project.
- Global installation makes the test runner available at the WebStorm level so it can be used in any WebStorm project. Moreover, during installation the parent folder of the test runner is automatically added to the
PATH
variable, which enables you to launch the test runner from any folder. - Because Cucumber.js is a test framework, which is of no need for those who are going to re-use your application, it is helpful to have it excluded from download for the future. This is done by marking the tool as a development dependency, which actually means adding the tool in the
devDependencies
section of thepackage.json
file.
- launch the embedded Terminal ( or by hovering your mouse pointer over in the lower left corner of WebStorm and choosing Terminal from the menu and type one of the following commands at the command line prompt:
-
npm install cucumber
to install Cucumber.js in your project. -
npm install -g cucumber
to install Cucumber.js globally. The-g
key makes the test runner run in the global mode. Because the installation is performed through NPM, the Cucumber.js test runner is installed in thenpm
folder. Make sure this parent folder is added to thePATH
variable. This enables you to launch the test runner from any folder. -
npm install --dev cucumber
to install Cucumber.js as a development dependency. After installation, the test runner is added to thedevDependencies
section of thepackage.json
file.
-
See NPM documentation for more details about installing the Cucumber.js test runner and npm operation modes.
You can also install the cucumber
package on the Node.js and NPM page of the Settings dialog box as described in Node Package Manager (npm).
Creating a Cucumber.js run configuration
- Open the Run/Debug Configuration dialog box by doing one of the following:
- Click the Add button on the toolbar and select the Cucumber.js configuration type. The Run/Debug Configuration: Cucumber.js dialog box opens.
- In the Feature file or directory text box, specify the tests to run. Cucumber.js runs tests that are called features and are written in the Gherkin language. Each feature is described in a separate file with the extension
feature
. - In the Executable path text box, specify the location of the cucumber executable file,
.cmd
,.bat
, or other depending on the operating system used. The location depends on the installation mode, see Installing the Cucumber.js test runner. - Optionally, specify the command line arguments to be passed to the Cucumber.js executable file, such as
-r
(--require LIBRARY|DIR
),-t
(--tags TAG_EXPRESSION
), or--coffee
. For details, see Cucumber's native built-in help available through thecucumber-js --help
command. - Apply the changes and close the dialog box.
Launching tests
- To launch the tests according to a run configuration, select the Cucumber.js run/debug configuration from the list on the main toolbar and click the Run button to the right of the list.
- The test server starts automatically without any steps from your side. View and analyze messages from the test server in the Run tool window.
- Monitor test execution in the Test Runner tab of the Run tool window as described in Monitoring and Managing Tests.