grunt-mock

0.0.3 • Public • Published

grunt-mock

npm version badge Build Status License

The grunt task is used to execute the specified mock-functions and save the result

In a unit test, mock objects can simulate the behavior of complex, real objects and are therefore useful when a real object is impractical or impossible to incorporate into a unit test.

Getting Started

This plugin requires Grunt ~0.4.5

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-mock --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-mock');

grunt-mock task

Run this task with the grunt mock command.

Task targets, files and options may be specified according to the grunt Configuring tasks guide.

Usage Example

module.exports = function (grunt) {
    grunt.initConfig({
        mock: {
            test: {
                files: {
                    'output.json': [ 'mocks/**/*.js' ]
                }
            }
        }
    });
 
    grunt.loadNpmTasks('grunt-mock');
    grunt.registerTask('default', ['mock']);
};

Input file

module.exports = {
    'mock#1': function () {
        return 1;
    },
 
    'mock#2': function () {
        return 2;
    }
};

Output file

{
    "mock#1": 1,
    "mock#2": 2
}

Options

process

Type: Function (data, original)
Default: (data, original) => original;

This option as an advanced way to control the file contents that are created.

module.exports = function (grunt) {
    grunt.initConfig({
        mock: {
            test: {
                options: {
                    process: function (data, original) {
                        var output = {};
 
                        data.forEach(function (mocks) {
                            for (var mock in mocks.data) {
                                output[mock] = mocks.data[mock](mock, mocks.file);
                            }
                        });
 
                        return JSON.stringify([ output ], null, '\t');
                    }
                },
 
                files: {
                    'output.json': [ 'mocks/**/*.js' ]
                }
            }
        }
    });
 
    grunt.loadNpmTasks('grunt-mock');
    grunt.registerTask('default', ['mock']);
};

Input file

module.exports = {
    'mock#1': function () {
        return 1;
    },
 
    'mock#2': function () {
        return 2;
    }
};

Output file

[
    {
        "mock#1": 1,
        "mock#2": 2
    }
]

Tests

grunt test

License

MIT

Links

Mock object

Task submitted by Alexander Abashkin

Package Sidebar

Install

npm i grunt-mock

Weekly Downloads

4

Version

0.0.3

License

none

Last publish

Collaborators

  • monolithed