swille
swille

Reputation: 841

How to run jasmine specs across multiple test directories

Jasmine is set up to run its specs under a single directory. It is not set up to find and run tests from multiple spec directories.

Here's my project structure:

 project root: /
     package.json
     spec
        --> jasmine_examples
     module_a
        --> spec
     module_b
        --> spec
     module_c
       --> spec

If I want to run tests from each of the modules, I have to specify each spec file.

    "spec_files": [
      "module_a/spec/spec.js",
      "module_b/spec/spec.js",
      "module_c/spec/spec.js"
    ],

This allows me to run the jasmine cli but it's not scalable. There must be a better way. I don't want to have to manually specify every module that contains a spec. I'd like to have all directories recursively searched for specs.

I'm only running these on Node using JSDom, no Karma or any headless browsers.

Upvotes: 5

Views: 8225

Answers (3)

Fill
Fill

Reputation: 392

Best way to approach this is to set "spec_dir" to root and then use spec_files and glob pattern to match multiple files inside different locations e.g.

{
  "spec_dir": "",
  "spec_files": [
    "module_a/**/*.[sS]pec.js",
    "module_b/**/*.[sS]pec.js",
    "module_c/**/*.[sS]pec.js"
  ],
  "stopSpecOnExpectationFailure": false,
  "random": true
}

Following config (located in jasmne.json) will match every "*.spec.js" file inside this module dirs.

You can improve this by moving all modules to common dir e.g. "src" and then change "jasmine.json" to:

{
  "spec_dir": "",
  "spec_files": [
    "src/**/*.[sS]pec.js"
  ],
  "stopSpecOnExpectationFailure": false,
  "random": true
}

This will let you forget once and for all about maintenance of this file... and we all know how we devs hate updating all those pesky configs ^^

Upvotes: 0

JJones
JJones

Reputation: 843

If you change your "spec_dir" property to the root with a double quote ("") then Jasmine will start at the root and traverse the sub-directories.

The problem then will be that the node_modules directory will also be included. So you'll have to use a spec pattern in spec_files that's highly unlikely to be be found in there. I used ["**/*.[sS]pec.js"] which meant naming my spec files module_a.spec.js.

{
  "spec_dir": "",
  "spec_files": [
    "**/*.[sS]pec.js"
  ],
  "helpers": [
    "helpers/**/*.js"
  ],
  "stopSpecOnExpectationFailure": false,
  "random": false
}

Upvotes: 4

scipper
scipper

Reputation: 3153

You could also seperate your spec files from your source root (what might be src/) to test/ with an equivalent folder structure:

project root: /
 package.json
 spec
    --> jasmine_examples
 src/
   module_a
   module_b
   module_c
 test/
   module_a
   module_b
   module_c

And then in your jasmine config define spec files like:

"spec_files": [
  "test/**/*.spec.js"
],

Upvotes: 2

Related Questions