Git Product home page Git Product logo

grunt-create-test-files's Introduction

grunt-create-test-files Build Status

Creates a template based test file for every source file

The purpose of this grunt plugin is to dynamically create a test file for each JavaScript source file that is added to your project. The generated test file is based on a lodash template that you provide, into which several properties based on the filename/path are made available for substitition. The path of the generated file will correspond to the directory structure of your source code tree. If necessary, new directories in your test tree will be created.

Recommended usage is to include this task in your grunt-contrib-watch tasks so test files will be created automatically as source files are added.

Test files are only generated if a file with the target path does not exist; existing test files will not be replaced.

Note, you can specify different templates for different filename patterns, e.g. in a MV* project you may want a specific test template for view files that construct, render, etc. whereas for models or collections you may want a test template with a different pattern.

Getting Started

This plugin requires Grunt ~0.4.2

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-create-test-files --save-dev

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

grunt.loadNpmTasks('grunt-create-test-files');

The "create_test_files" task

Overview

In your project's Gruntfile, add a section named create_test_files to the data object passed into grunt.initConfig().

grunt.initConfig({
  create_test_files: {
    your_target: {
      options: {
        templateFile: 'path/to/your/template.file',
        destinationBasePath: 'root/path/of/test/dir/',
        sourceBasePath: 'root/path/of/src/dir/'
      },
      files: {
        src: 'glob/pattern/to/src/files/relative/to/sourceBasePath/option/e/g/**/*.js'
      }
    },
  },
});

Options

options.templateFile

Type: String Required

Path to the template to use to generate test files. File will be processed as a lodash template; the following properties (describing the source file under test) are provided to the template for substitution:

  • path full path (relative to sourceBasePath option) and filename of file under test
  • amdPath same as path with *.js suffix truncated
  • filename filename, without path, of file under test
  • name filename, without path, with *.js suffix truncated, of file under test
  • capitalizedName same as name with uppercase first character
  • camelizedName similar to name, but with dashes and underscores stripped and converted to camelCase
  • capitalizedCamelizedName same as camelizedName with uppercase first character

Note, you may use these sample templates (please issue a pull request to contribute your own).

options.destinationBasePath

Type: String Default value: 'test/'

The path to the root directory of where generated files will be created. The paths of the generated files, relative to this directory, will match the path of the corresponding source file, relative to the sourceBasePath option.

options.sourceBasePath

Type: String Default value: 'main/'

The path to the root directory of the source files to be matched. The filename match pattern provided in the files parameter must be relative to this path.

options.testFileSuffix

Type: String Default value: 'Spec.js'

The filename of the generated test file is created by taking the name of the corresponding source file and replacing the trailing '.js' with the testFileSuffix. Thus with the default value, the test file for main.js will be named mainSpec.js

Usage Examples

Default Options

Given a file directory structure such as the below:

/
|-- Gruntfile.js
|-- main/
|   |-- index.js
|   |-- models/
|   |   +-- fooModel.js
|   |
|   +-- lib/
|       +-- myFavoriteMvcLib.js
|
+-- test/
    |-- indexSpec.js
    +-- models/
        +-- fooModelSpec.js

And the configuration below:

grunt.initConfig({
  create_test_files: {
    options: {
      templateFile: 'test/templates/spec.template',
    },
    files: {
      src: [
        'main/**/*.js',
        '!main/lib/**/*.js'
      ]
    },
  },
});

And the content of spec.template as below:

// path: ${path}
// filename: ${filename}
define(['${amdPath}'], function(${capitalizedName}) {
  'use strict';

  describe('${amdPath}', function() {
    var ${name};
    beforeEach(function() {
      ${name} = new ${capitalizedName}();
    });
    describe('constructor', function() {
    });

  });
  
});

If indexSpec.js does not yet exist, it will be created. If the test/models directory does not yet exist, it will be created. Likewise for fooModelSpec.js

The content of generated file fooModelSpec.js would look like:

// path: models/fooModel.js
// filename: fooModel.js
define(['models/fooModel'], function(FooModel) {
  'use strict';

  describe('models/fooModel', function() {
    var fooModel;
    beforeEach(function() {
      fooModel = new FooModel();
    });
    describe('constructor', function() {
    });

  });
  
});

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

  • 2014-02-21 v0.1.1 initial release
  • 2014-02-21 v0.1.2 fix links in package.json
  • 2014-03-05 v0.1.3 add template substitution for camelizedName and capitalizedCamelizedName

grunt-create-test-files's People

Contributors

bruceharris avatar

Watchers

Lam Nguyen avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.