vpi
vpi

Reputation: 21

Grunt : Running multiple mocha tests with different environments/processes

I want to run multiple mocha tests on different environments and databases.

A tentative is to create two separate grunt configurations:

grunt.initConfig({
    // first one
    mochaTest: {
       options: { ... },
       src: ['test/server/*.js']
    },
    // second one
    mochaTest: {
        options: { ... },
        src: ['test/slave/*.js']
    },
    env: {
        dev: { NODE_ENV: 'development' },
        test: { NODE_ENV: 'test' }
    },
});

grunt.registerTask('test', function (target) {
    if (target === 'server') {
        return grunt.task.run([
            'env:test',
            'mochaTest' // the first one
        ]);
    }
    if (target === 'slave') {
        return grunt.task.run([
            'env:dev',
            'mochaTest' // the second one
        ]);
    }
    grunt.task.run([
        'test:server',
        'test:slave'
    ]);
});

Of course, it doesn't work the two config have the same name. I tried to rename the second config by mochaTest2but it is not recognized by grunt.

Then, I tried to put it in the same mochaTest task but the test are run in the same environment and process.

grunt.initConfig({
    mochaTest: {
        server: {
            options: { ... },
            src: ['test/server/*.js']
        },
        slave: {
            options: { ... },
            src: ['test/slave/*.js']
        }
    },
    env: {
        dev: { NODE_ENV: 'development' },
        test: { NODE_ENV: 'test' }
    },
});

grunt.registerTask('test', function (target) {
    if (target === 'server') {
        return grunt.task.run([
            'env:test',
            'mochaTest:server'
        ]);
    }
    if (target === 'slave') {
        return grunt.task.run([
            'env:dev',
            'mochaTest:slave'
        ]);
    }
    grunt.task.run([
        'test:server',
        'test:slave'
    ]);
});

The ideal solution should run two mochaTest (one for server, the other for slave) each in a separate process.

Upvotes: 2

Views: 924

Answers (2)

Artūrs Sosins
Artūrs Sosins

Reputation: 579

I know it is an old question, but since I stumbled on it myself while looking for the same answer, and turns out grunt-mocha-test supports it with sub objects.

For example, here you can define running server and slave tests like this:

grunt.initConfig({
    ... // skipping other details 
    mochaTest : {
        server : {
            options: { ... },
            src: ['test/server/*.js']
        },
        slave : {
            options: { ... },
            src: ['test/slave/*.js']
        }
    }
});

And then you can run them by specifying sub object through ":", like

grunt mochaTest:server

or

grunt.registerTask('slave', ['jshint', 'mochaTest:slave']);

More information: https://www.polyglotdeveloper.com/cookbook/2016-03-03-How-to-run-multiple-test-files-in-different-groups-from-mocha/

Upvotes: 1

GreensterRox
GreensterRox

Reputation: 7120

This might be a bit unorthodox but you can get around the issue you describe with not being able to call 'mochaTest' twice by using two separate plugins that are both capable of running mocha tests.

Install:

npm install -save grunt-simple-mocha
npm install --save grunt-mocha-test

Load:

grunt.loadNpmTasks('grunt-mocha-test');
grunt.loadNpmTasks('grunt-simple-mocha');

Configure:

mochaTest: {
      test: {
        src: ['test/*serverTests*.js']
      }
    },

simplemocha: {
      options: {
        ui: 'bdd'
      },
      all: { src: ['test/*slaveTests*'] }
    }

Register tasks:

grunt.registerTask('serverTests', ['mochaTest']);
grunt.registerTask('slaveTests', ['simplemocha']);
grunt.registerTask('allTests', ['mochaTest', 'simplemocha']);

Execute:

grunt serverTests  // run just server tests
grunt slaveTests   // run just slave tests
grunt allTests     // run both server and slave tests

Upvotes: 1

Related Questions