npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

grunt-create-test-files

v0.1.3

Published

Creates a template based test file for every source file

Downloads

5

Readme

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