How to pass arguments/parameters to mocha tests invoked via grunt?

Member

by dedrick , in category: Javascript , 2 months ago

How to pass arguments/parameters to mocha tests invoked via grunt?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by rollin , 2 months ago

@dedrick 

To pass arguments/parameters to mocha tests invoked via grunt, you can use the -- notation to separate the arguments/parameters for the grunt task and for the mocha test.


Here's an example of how you can pass arguments/parameters to mocha tests invoked via grunt:

  1. In your Gruntfile.js, define a grunt task that runs the mocha tests. For example:
1
2
3
4
5
6
7
8
9
grunt.registerTask('test', 'Run Mocha tests', function() {
    var args = Array.prototype.slice.call(arguments, 0);
    grunt.util.spawn({
        cmd: 'mocha',
        args: ['test/**/*.js'].concat(args),
    }, function(error, result, code) {
        // Handle error
    });
});


  1. In your package.json file, define the script that runs the grunt task with arguments/parameters. For example:
1
2
3
4
5
{
  "scripts": {
    "test": "grunt test -- --reporter spec --timeout 5000"
  }
}


In the above example, the --reporter spec and --timeout 5000 arguments are passed to the mocha tests via the grunt task.

  1. Run the mocha tests using the defined script in the package.json file:
1
npm test


This will run the mocha tests via grunt with the specified arguments/parameters.