grunt-create-test-files

0.1.3 • Public • Published

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

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.1.3
    1
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 0.1.3
    1
  • 0.1.2
    0
  • 0.1.1
    0
  • 0.1.0
    0

Package Sidebar

Install

npm i grunt-create-test-files

Weekly Downloads

1

Version

0.1.3

License

none

Last publish

Collaborators

  • bruceharris