Specify QUnit module when run with Grunt
Asked Answered
A

2

6

I'm using Grunt, PhantomJS, and the "watch" plugin to run my QUnit tests while I develop (separate from CI). I'd like to be able to focus on a specific QUnit module while I'm working on the code that that module's tests are focused on. When running QUnit in the browser I can specify a module to run (versus all tests).

So the question is, can I tell the Grunt qunit task to only run a certain module? I'm thinking a command line argument so that I don't have to change my Gruntfile, something like:

~$ grunt qunit --module="test this stuff, test that stuff"

UPDATE

To be clear, what I want to run are modules created in a test suite using QUnit's module() method:

module( "group a" );
test( "a basic test example", function() {
    ok( true, "this test is fine" );
});
test( "a basic test example 2", function() {
    ok( true, "this test is fine" );
});

module( "group b" );
test( "a basic test example 3", function() {
    ok( true, "this test is fine" );
});
test( "a basic test example 4", function() {
    ok( true, "this test is fine" );
});

In the example above, this code is all in one test suite, but in the resulting html test file I get a drop down to run either module "group a" or module "group b" (through QUnit's UI). What I want is to be able to specify, programmatically, that I want to run a specific module through the grunt qunit task.

Andi answered 26/4, 2013 at 16:28 Comment(0)
K
0

If you set up your config for grunt-qunit like this:

grunt.initConfig({
  qunit: {
    module1: {
      options: {
        urls: [
         'http://localhost:8000/test/module1/foo.html',
         'http://localhost:8000/test/module1/bar.html',
        ]
      }
    },
    module2: {
      options: {
        urls: [
         'http://localhost:8000/test/module2/foo.html',
         'http://localhost:8000/test/module2/bar.html',
        ]
      }
    }
  }
  ...

You can run an individual module like grunt qunit:module1

Khoury answered 31/12, 2013 at 19:56 Comment(4)
While true, I was hoping to find something more generic. Basically, when I add a module to my test suite, I don't want to have to go create a new module target for it. Thanks for posting though.Andi
Well, you can make a task that looks in ./test and configs up a module for each subdirectory then queues them for execution, and takes a modules argument to filter on.Khoury
Yes, but I'm more referring to being able to run QUnit's modules. What you have here is the creation of different html test suites that act in that role. What I want though is to be able to run the module that is declared in the test suite, not a newly created html file.Andi
That functionality doesn't seem to exist in grunt-contrib-qunit, so I think you'd have to submit a patch or feature request.Khoury
I
0

I think a viable workaround could be to define a module filter option, and if it exist, append it to the urls. Something like this in the Gruntfile.js

var moduleFilter =  '';
if (grunt.option('module')) {
  moduleFilter = '?module=' + grunt.option('module')
}

then using it:

grunt.initConfig({
  qunit: {
    options: {
        ...
        urls: [
          'http://localhost:3000/qunit' + moduleFilter
        ]
    }
  }
});

Note that this will work for one module only. Maybe (but not tested) you can use the filter query param instead of module, and name your modules to match the filter, in order to be able to group them.

Note: Running multiple modules is not something that QUnit will support.

References:

Ibbie answered 1/7, 2014 at 14:52 Comment(1)
That certainly looks like a more workable solution, I'll try it out when I'm back on this project.Andi

© 2022 - 2024 — McMap. All rights reserved.