Thejasmine
module is a command line interface and supporting code for runningJasmine specs under Node.js. Jasmine 5.x supports Node versions 18, 20,and 22. (Odd-numbered Node versions aren’t supported, but many of them work.)
You can install Jasmine using npm locally in your project:
npminstall--save-dev jasmine
With the above local installation you can invoke the CLI tool usingnpx jasmine ...
commands.
Optionally you can also install jasmine globally so that you can invoke theCLI tool withoutnpx
. This is not recommended, however, since it’s difficultto keep the globally installedjasmine
version in sync with each project thatuses it.
npminstall-g jasmine
Initialize a project for Jasmine by creating a spec directory and configuration json for you:
npx jasmine init
Generate example spec and source files:
npx jasmine examples
At this point you should be able towrite your first suite.
Customizespec/support/jasmine.json
to enumerate the source files and spec files you would like theJasmine runner to include. You may use dir glob strings.
Paths starting with!
are excluded, for example!**/*nospec.js
.
spec_dir
is used as a prefix for allspec_files
andhelpers
.Helpers are executed once before all specs. For an example of some helpers see theReact tutorial.
{// Spec directory path relative to the current working dir when jasmine is executed.// The value "" represents the current working directory."spec_dir":"spec",// Array of filepaths (and globs) relative to spec_dir to include and exclude"spec_files":["**/*[sS]pec.?(m)js","!**/*nospec.js"],// Array of filepaths (and globs) relative to spec_dir to include before jasmine specs"helpers":["helpers/**/*.?(m)js"],// Configuration of the Jasmine environment// "env" is optional, as are all of its properties."env":{// Whether to fail a spec that ran no expectations"failSpecWithNoExpectations":false,// Stop execution of a spec after the first expectation failure in it"stopSpecOnExpectationFailure":false,// Stop execution of the suite after the first spec failure"stopOnSpecFailure":false,// Run specs in semi-random order"random":false}}
You can also specify a different config file by using either the--config
command line argument or theJASMINE_CONFIG_PATH
environment variable, asfollows. Config files may be either.json
or.js
. A.js
config fileshould be a module whose default export is a configuration object.
jasmineJASMINE_CONFIG_PATH=relative/path/to/your/jasmine.jsonjasmine--config=relative/path/to/your/jasmine.json
Once you have set up yourjasmine.json
, you can execute all your specs by runningjasmine
from the root of your project (ornpx jasmine
if you had installed it locally).
If you want to just run one spec or only those in files that match a certainglob pattern you can do it like this:
npx jasmine spec/appSpec.jsnpx jasmine"**/model/**/critical/**/*Spec.js"
Execute only those specs which filename match given glob:
npx jasmine"spec/**/critical/*Spec.js"
Or a single file:
npx jasmine spec/currentSpec.js
Or execute only those specs which name matches a particular regex:
npx jasmine--filter"adapter21*"
(where thename of a spec is the first parameter passed todescribe()
)
Jasmine loads your code using dynamic import, which should be compatible withbothES modules andCommonJS modules. Thismeans that a script will be loaded as an ES module if its name ends in.mjs
orif thepackage.json
of the package containing the file contains"type": "module"
.
The default configuration should work fine for nearly all CommonJS projects aswell as those that use ES modules. But if necessary you can configure Jasmineto load scripts usingrequire
by adding"jsLoader": "require"
to yourJasmine config file. If you have code that works with"jsLoader": "require"
but not without it, pleaselet us know.Files with names ending in.mjs
will be loaded via dynamic import even ifjsLoader
is set to"require"
.
JASMINE_CONFIG_PATH=
Specify a relative or absolute path to your configuration file. Can be used as an option or set as an environment variable.
JASMINE_CONFIG_PATH=spec/config/jasmine.json jasminenpx jasmine--config=spec/config/jasmine.json
--no-color
Turns off color in spec output
npx jasmine--no-color
--filter=
Only runs specs that match the given string
npx jasmine--filter="a spec name"
--fail-fast
Stops execution of the suite after the first expectation failure or other error
npx jasmine--fail-fast=true
--random=[true|false]
Tells jasmine to run specs in semi random order or not for this run, overridingjasmine.json
npx jasmine--random=true
--seed=
Sets the randomization seed if randomization is turned on
npx jasmine--seed=4321
--reporter=
Sets the default reporter. The value must be a validimport specifierfor a module whose default export is a reporter constructor.
npm i--save-dev jasmine-ts-console-reporternpx jasmine--reporter=jasmine-ts-console-reporter
If you want more granular control over the configuration, Jasmine can also be used as a library in your project.This allows you to load multiple config files or control your configuration in different ways.
constJasmine=require('jasmine');construnner=newJasmine();
awaitrunner.loadConfigFile('spec/support/jasmine.json');runner.loadConfig({spec_dir:'spec',spec_files:['appSpec.js','requests/**/*[sS]pec.js','utils/**/*[sS]pec.js'],helpers:['helpers/**/*.js']});
By default, Jasmine will cause the Node process to exit once the suite finishesrunning. The exit code will be 0 if theoverall statusof the suite is'passed'
and nonzero in all other cases. If you want to handlecompletion differently, you can set the Jasmine instance’sexitOnCompletion
property tofalse
and use the promise returned fromexecute
. This is often used to message a status to task runners like grunt.
runner.exitOnCompletion=false;constresult=awaitrunner.execute();if(result.overallStatus==='passed'){console.log('All specs have passed');}else{console.log('At least one spec has failed');}
A ConsoleReporter is included if no other reporters are added.You can configure the default reporter withconfigureDefaultReporter
.The default values are shown in the example.
runner.configureDefaultReporter({// The `timer` passed to the reporter will determine the mechanism for seeing how long the suite takes to run.timer:newjasmine.jasmine.Timer(),// The `print` function passed the reporter will be called to print its results.print:function(){process.stdout.write(arguments);},// `showColors` determines whether or not the reporter should use ANSI color codes.showColors:true});
You can add a custom reporter withaddReporter
. If you add a reporter throughaddReporter
, the default ConsoleReporter will not be added.Multiple reporters can be added.
constCustomReporter=require('./myCustomReporter');runner.addReporter(newCustomReporter());
Callingexecute
will run the specs.
runner.execute();
execute
can optionally be called with a list of spec file paths to execute relative to the current working directory and a string to filter by spec name.
runner.execute(['fooSpec.js'],'a spec name');
// CommonJSconstJasmine=require('jasmine');construnner=newJasmine();runner.loadConfigFile('spec/support/jasmine.json').then(function(){runner.configureDefaultReporter({showColors:false});runner.execute();});// or ESM:importJasminefrom'jasmine';construnner=newJasmine();awaitrunner.loadConfigFile('spec/support/jasmine.json');runner.configureDefaultReporter({showColors:false});runner.execute();