Fixing the Jasmine “No Specs Found” Error

There are a number of steps in setting up Jasmine for unit testing Javascript code. Skipping one simple step in this process can result in a puzzling “No specs found” error when trying to run tests in a newly created project.

A previous post described how to use Jasmine to unit test Javascript code. Recently there have been a lot of visits to that post from internet searches for “jasmine no specs found” or similar. This post will address a common cause of this issue.

You think you’ve done everything correctly:

  • Installed jasmine under your project (“npm install –save-dev jasmine” or “yarn add –dev jasmine”)
  • Created a “spec” subdirectory in the project and put a first unit test there
  • Defined a convenience “test” script in package.json to run “jasmine” (add ‘”test”: “jasmine”‘ to “scripts: { … }” there)

But when you run ‘npm test’ or ‘yarn test’ on the command line, the test fails with a mysterious “No Specs Found” error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ yarn test
yarn run v1.5.1
$ jasmine
Randomized with seed 71141
Started
 
 
No specs found
Finished in 0.002 seconds
Incomplete: No specs found
Randomized with seed 71141 (jasmine --random=true --seed=71141)
error An unexpected error occurred: "Command failed.
Exit code: 1
Command: sh
Arguments: -c jasmine

If this was due to a Javascript syntax error, jasmine would show some details of the cause. But the code in the test spec (as well as the code under test) looks OK.

It’s sometimes the simple things that make the difference. The key step that was missed above is to initialize jasmine for the project. Running “yarn test init” (or the same under “npm” or “jasmine init” if installed globally) creates a “jasmine.json” config file under a “spec/support” subdirectory that, among other things, tells jasmine where to find the specs to be tested:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ yarn test init
yarn run v1.5.1
$ jasmine init
  Done in 0.23s.
 
$ cat spec/support/jasmine.json 
{
  "spec_dir": "spec",
  "spec_files": [
    "**/*[sS]pec.js"
  ],
  "helpers": [
    "helpers/**/*.js"
  ],
  "stopSpecOnExpectationFailure": false,
  "random": true
}

It would be nice if Jasmine warned that it couldn’t find a config file, but it doesn’t – it simply displays the “no specs were found” message. And indeed, without the “spec_dir” and “spec_files” configurations, there is no way for Jasmine to find the specs.

With the jasmine config file present, the unit test specs work correctly:

1
2
3
4
5
6
7
8
9
10
11
12
$ yarn test
yarn run v1.5.1
$ jasmine
Randomized with seed 11534
Started
.
 
 
1 spec, 0 failures
Finished in 0.008 seconds
Randomized with seed 11534 (jasmine --random=true --seed=11534)
   Done in 0.24s.

Jasmine is set up to default to looking for the config file at this “./spec/support/jasmine.json” location (see the code under node_modules/jasmine/tasks/jasmine.js). A “–config” option can be passed to the jasmine command or a JASMINE_CONFIG_PATH environment variable can be set up to find the config file in a different location, but the “jasmine.json” file must be there.

TL; DR

It’s easy to forget to do a “jasmine init” (or “yarn test init” or “npm test init” depending on your setup) to create the necessary “jasmine.json” config file when you’re rushing through to get unit tests working for a new javascript project. Without this config file, running the test specs will result in a “No specs found” error without further explanation of what is wrong.

The complete summary of steps for setting up Jasmine unit testing is:

  • Install jasmine under your project (“npm install –save-dev jasmine” or “yarn add –dev jasmine”)
  • Create a “spec” subdirectory in the project and put a first unit test there
  • Define a convenience “test” script in package.json to run “jasmine” (add ‘”test”: “jasmine”‘ to “scripts: { … }” there)
  • Run ‘npm test init’ or ‘yarn test init’ to initialize Jasmine, creating the “jasmine.json” config file (use ‘jasmine init’ if Jasmine is installed globally)
  • Run the unit test with ‘npm test’ or ‘yarn test’ and verify that it is working correctly

Jasmine is hard coded to look for specs at “spec/support/jasmine.json” in the project (starting from where “project.json” and “node_modules” is located). A different config file can be used by specifying a “–config” option to jasmine or defining a JASMINE_CONFIG_PATH environment variable.

Versions

$ yarn test -v
yarn run v1.5.1
jasmine v3.3.1
jasmine-core v3.3.0

Add a Comment

Your email address will not be published.