Mocha Adapter for Nightwatch.js
The `mocha-nightwatch` package serves as an adapter, enabling the use of Mocha's familiar BDD/TDD syntax, such as `describe` and `it`, within the Nightwatch.js end-to-end testing framework. This allows developers to structure their Nightwatch tests using Mocha's expressive assertion style and test organization patterns. The package is currently at version 3.2.2. However, it is important to note that this adapter has not been updated in over five years, with its last publication in October 2019. It relies on significantly older versions of its core dependencies, specifically Mocha (~3.2.0) and Nightwatch.js (~0.9.12). Consequently, its release cadence is non-existent, and it is incompatible with modern versions of Node.js, Nightwatch.js (v1.x, v2.x, v3.x+), and Mocha (v4.x+). Key differentiators, at the time of its relevance, included bridging two popular testing paradigms for browser automation, but its abandonment makes it unsuitable for current development.
Common errors
-
Error: The `nightwatch` dependency is not installed.
cause Nightwatch.js is either missing from the project or installed at an incompatible version that the adapter cannot detect.fixEnsure `nightwatch@~0.9.12` is explicitly installed in your project: `npm install nightwatch@~0.9.12`. -
ReferenceError: describe is not defined
cause The Mocha test runner environment has not been correctly initialized, preventing global test functions from being exposed.fixVerify that `test_runner.type` is explicitly set to 'mocha' in your `nightwatch.conf.js` file and that `mocha@~3.2.0` is installed as a dependency. -
TypeError: browser.url is not a function
cause This error or similar 'browser' object method failures typically indicate an incompatibility between the old Nightwatch.js API expected by the adapter and a newer Nightwatch.js version being used.fixThis adapter strictly supports Nightwatch.js <=0.9.12. Ensure `nightwatch@~0.9.12` is installed and that your `nightwatch.conf.js` correctly configures the Mocha test runner for this specific version.
Warnings
- breaking This package is incompatible with Nightwatch.js versions 1.x, 2.x, or 3.x and relies on the severely outdated Nightwatch.js ~0.9.12 API.
- breaking This adapter is not compatible with Mocha versions 4.x and above, as it was designed for Mocha ~3.2.0. Significant breaking changes occurred in later Mocha releases.
- gotcha The `mocha-nightwatch` package is effectively abandoned, with its last update over five years ago. It is highly unlikely to function correctly with modern Node.js versions, browser versions, or current versions of its dependencies.
- gotcha The package requires Node.js >=0.10.x, an end-of-life Node.js release. Running it on modern Node.js LTS versions (e.g., v14+) will likely result in runtime errors or unexpected behavior due to API changes and deprecations.
Install
-
npm install mocha-nightwatch -
yarn add mocha-nightwatch -
pnpm add mocha-nightwatch
Imports
- describe
import { describe } from 'mocha-nightwatch'/* Used globally or implicitly by Mocha test runner */
- it
import { it } from 'mocha-nightwatch'/* Used globally or implicitly by Mocha test runner */
- browser
import { browser } from 'mocha-nightwatch'/* Injected as a parameter into test functions */
Quickstart
// nightwatch.conf.js
const chromedriver = require('chromedriver');
module.exports = {
src_folders: ["test"],
output_folder: "reports",
globals_path: "globals.js",
test_runner: {
type: "mocha",
options: {
ui: "bdd",
reporter: "spec",
timeout: "60000"
}
},
webdriver: {
start_process: true,
port: 9515,
server_path: chromedriver.path, // Requires 'chromedriver' package
cli_args: [
"--verbose"
]
},
test_settings: {
default: {
launch_url: "http://localhost",
desiredCapabilities: {
browserName: "chrome",
javascriptEnabled: true,
acceptSslCerts: true,
chromeOptions: {
args: ["--headless"]
}
}
}
}
};
// test/example.spec.js
describe('Google Homepage Test', function() {
it('should have a title', function(browser) {
browser
.url('https://www.google.com')
.waitForElementVisible('body', 1000)
.assert.titleContains('Google')
.end();
});
it('should find the search input', function(browser) {
browser
.url('https://www.google.com')
.waitForElementVisible('input[name="q"]', 1000)
.assert.elementPresent('input[name="q"]')
.end();
});
});