Jasmine-Node Test Runner
Jasmine-node is a command-line utility designed for running Jasmine BDD specifications (specifically version 1.3.1, a forked version from the Karma project) within a Node.js environment without requiring a browser DOM. It provides a simple CLI for executing tests written in JavaScript or CoffeeScript, offering features like automatic test execution on file changes (`--autotest`), colorized output, and optional Growl notifications. The project is explicitly in maintenance mode, and its authors recommend using the official `jasmine` or `jasmine-npm` packages for any new development due to `jasmine-node`'s reliance on an older Jasmine version and limited future development. It supports Node.js versions 10 and 12, with older Node.js versions being deprecated.
Common errors
-
ReferenceError: describe is not defined
cause This error occurs when a test file is executed directly using `node` (e.g., `node spec/my-test-spec.js`) instead of through the `jasmine-node` command-line runner. The runner is responsible for setting up Jasmine's global functions like `describe`, `it`, and `expect`.fixAlways run your tests using the `jasmine-node` CLI command, specifying the directory where your specs are located (e.g., `jasmine-node spec/`). -
No specs found
cause This usually indicates that `jasmine-node` could not locate any test files. Common causes include spec files not being in the specified directory, or their filenames not matching the required `*spec.js` (or `.coffee`, `.litcoffee`) pattern.fixVerify that your spec files are correctly placed in the directory you're passing to `jasmine-node` (e.g., `spec/`) and that their names conform to the `*spec.js` pattern. If you've used non-standard naming, try running with the `--matchall` flag.
Warnings
- breaking The `jasmine-node` project is in maintenance mode and relies on an outdated Jasmine 1.3.1. It is highly recommended to migrate to the actively maintained official `jasmine` or `jasmine-npm` packages for modern testing environments and newer Jasmine versions (2.x+).
- deprecated Support for Node.js versions 8, 6, and 4 is deprecated and has reached End-of-Life. The package only officially supports Node.js versions 10 and 12.
- gotcha The `Jasmine2.0` branch, which aimed to support Jasmine 2.0 with a CoffeeScript rewrite, has been explicitly abandoned and is no longer supported. Do not attempt to use it or expect Jasmine 2.x features within `jasmine-node`.
- gotcha Spec files must adhere to a specific naming convention to be discovered by the runner: their filenames must match the regular expression `/spec\.(js|coffee|litcoffee)$/i`. This means they typically end with `spec.js`, `spec.coffee`, or `spec.litcoffee`.
Install
-
npm install jasmine-node -
yarn add jasmine-node -
pnpm add jasmine-node
Imports
- CLI Tool Execution
import { jasmineNode } from 'jasmine-node'Install globally: npm install jasmine-node -g Run tests: jasmine-node spec/
- describe, it, expect
import { describe, it, expect } from 'jasmine-node'// These functions are made globally available by the jasmine-node runner when executing tests.
- Programmatic Runner Access
import * as jasmineNode from 'jasmine-node'
const cliRunner = require('jasmine-node/lib/jasmine-node/cli'); // Highly discouraged, use CLI instead
Quickstart
npm install jasmine-node -g
// Create a spec file (e.g., 'spec/example-spec.js')
// NOTE: The filename MUST end with 'spec.js', 'spec.coffee', or 'spec.litcoffee'
describe('A basic Jasmine suite', function() {
// Synchronous test
it('should verify true is true', function() {
expect(true).toBe(true);
});
// Asynchronous test with 'done'
it('should handle async operations', function(done) {
setTimeout(function() {
expect('async').toBe('async');
done();
}, 50);
});
// Using ddescribe to run only this suite (feature from Karma fork)
ddescribe('A focused suite example', function() {
it('should be the only test suite executed', function() {
expect(1).not.toBe(2);
});
});
});
// Run tests from the command line in your project's root directory
jasmine-node spec/