test/helpers

Collection of unit test helpers. (mostly related to Mocha syntax)

Methods

static create(GeneratorOrNamespace) → {RunContext}

Prepare a run context

Parameters:
Name Type Description
GeneratorOrNamespace String | function

Generator constructor or namespace

Returns:
RunContext

static createDummyGenerator()

Create a simple, dummy generator

static createEnv(…args) → {Object}

Shortcut to the Environment's createEnv.

Parameters:
Name Type Attributes Description
args any <repeatable>

environment constructor arguments.

Returns:
Object -

environment instance

Use to test with specific Environment version: let createEnv; before(() => { createEnv = stub(helper, 'createEnv').callsFake(Environment.creatEnv); }); after(() => { createEnv.restore(); });

static createGenerator(name, dependencies, args, options, localConfigOnlyopt)

Create a generator, using the given dependencies and controller arguments Dependecies can be path (autodiscovery) or an array [{generator}, {name}]

Parameters:
Name Type Attributes Default Description
name String

the name of the generator

dependencies Array

paths to the generators dependencies

args Array | String

arguments to the generator; if String, will be split on spaces to create an Array

options Object

configuration for the generator

localConfigOnly Boolean <optional>
true

passes localConfigOnly to the generators

Example
var deps = ['../../app',
             '../../common',
             '../../controller',
             '../../main',
             [createDummyGenerator(), 'testacular:app']
           ];
var angular = createGenerator('angular:app', deps);

static createMockedGenerator()

Create a mocked generator

static createTestEnv(envContructor, optionsopt) → {Object}

Creates a test environment.

Parameters:
Name Type Attributes Description
envContructor function

environment constructor method.

options Object <optional>

Options to be passed to the environment

Returns:
Object -

environment instance const env = createTestEnv(require('yeoman-environment').createEnv);

static gruntfile(options, done)

Generates a new Gruntfile.js in the current working directory based on options hash passed in.

Parameters:
Name Type Description
options Object

Grunt configuration

done function

callback to call on completion

Example
before(helpers.gruntfile({
  foo: {
    bar: '<config.baz>'
  }
}));

static mockLocalConfig(generator, localConfig)

Provide mocked values to the config

Parameters:
Name Type Description
generator Generator

a Yeoman generator

localConfig Object

localConfig - should look just like if called config.getAll()

static mockPrompt(generator, answers, options)

Answer prompt questions for the passed-in generator

Parameters:
Name Type Description
generator Generator | Environment

a Yeoman generator or environment

answers Object

an object where keys are the generators prompt names and values are the answers to the prompt questions

options function | Object

Options or callback

Example
mockPrompt(angular, {'bootstrap': 'Y', 'compassBoostrap': 'Y'});

static registerDependencies(dependencies)

Register a list of dependent generators into the provided env. Dependecies can be path (autodiscovery) or an array [{generator}, {name}]

Parameters:
Name Type Description
dependencies Array

paths to the generators dependencies

static restorePrompt(generator)

Restore defaults prompts on a generator.

Parameters:
Name Type Description
generator Generator | Environment

or environment

static run(GeneratorOrNamespace) → {RunContext}

Run the provided Generator

Parameters:
Name Type Description
GeneratorOrNamespace String | function

Generator constructor or namespace

Returns:
RunContext

static setUpTestDirectory(dir) → {function}

Create a function that will clean up the test directory, cd into it, and create a dummy gruntfile inside. Intended for use as a callback for the mocha before hook.

Parameters:
Name Type Description
dir String

path to the test directory

Returns:
function -

mocha callback

static testDirectory(dir, cb)

Clean-up the test directory and cd into it. Call given callback after entering the test directory.

Parameters:
Name Type Description
dir String

path to the test directory

cb function

callback executed after setting working directory to dir

Example
testDirectory(path.join(__dirname, './temp'), function () {
  fs.writeFileSync('testfile', 'Roses are red.');
});