chfw
chfw

Reputation: 4592

How to programmatically call yeoman generator?

I need some hints to write a script to answer prompts of yeoman generator.

Here's the background. I created my own generator using yeoman's generator-generator. I added a few prompts. In order to test the template project now and during future development, I need to generate a sample project and test the evolving template. Because I want hudson to do test routine, I need to automate it(answer those prompts by a script)

For my needs, I failed to find relevant information in the following sources:

  1. http://yeoman.github.io/generator/
  2. https://github.com/yeoman/generator, where there are some generator-invoking code in tests but I don't know how to run it using grunt or node scripts.

Has anyone done this before?

--Update 30/April--

I tried the following script as 'test/test-app.js':

var path = require('path');
var helpers = require('yeoman-generator').test;

describe('viena', function(){
  var options = { 'skip-install': true };
  var prompts = { 'scriptAppName': 'Viena' , "appTitle": "Test"};

  // Runs once:
  before(function(done){
    // This method creates temporary folder for test output
    // and clears it afterwards.
    helpers.setUpTestDirectory(path.join(__dirname, 'tmp'));
    done();
  });

  it('tests something', function(done){
    // Emulating user's prompts:
    helpers.run(path.join(__dirname, '../generators/app')).withPrompts(prompts);
    done();
  });
});

Then did:

npm test

But I failed to get hold of the generated app. I need to get the app and run the tests that are designed for the app. I don't want to check if some files were copied or not but I want to verify if the generated app pass the tests or not. Those tests belong to the app not the generator.

Upvotes: 1

Views: 1442

Answers (2)

Simon Boudrias
Simon Boudrias

Reputation: 44669

There's a couple things here.

The main one is please read the Yeoman testing documentation. There's lot of details an example on how to test and make assertion against the generated app.

Next is some of the methods you called here are asynchronous. The done callback is only to be called when these task are done running. In your case, you call it synchronously. So the generator start running, but you don't let it finish, you stop the test right away.

Upvotes: 0

sobolevn
sobolevn

Reputation: 18100

If I got it right you are tring to start tests from the CI (kind of).

First of all, ensure that you have something similar to this in your package.json:

"scripts": {
  "test": "mocha"
}

Change mocha to your custom test command.

Then in your test/test-app.js:

var helpers = require('yeoman-generator').test;

describe('your-generator:app', function(){
  var options = { 'skip-install': true };
  var prompts = { 'promptName': 'promptValue' };

  // Runs once:
  before(function(done){
    // This method creates temporary folder for test output
    // and clears it afterwards.
    helpers.setUpTestDirectory(path.join(__dirname, 'tmp'));
    done();
  });

  // Runs before every test case:
  beforeEach(function(done){
    // This method specifies the working directory for test case:
    helpers.testDirectory(path.join(__dirname, 'tmp'), function(err){
      if (err){
        return done(err);
      }

      // This is yeoman's method for initializing a generator instance:
      yourApp = helpers.createGenerator(
        'your-generator:app', ['../../app'], false, options
      );
      done();

    }.bind(this));
  });

  it('tests something', function(done){
    // Emulating user's prompts:
    helpers.mockPrompt(yourApp, prompts);
    yourApp.run(function(){  // Runing your generator.
      // assert something
      done();
    });
  });
});

Then write a script for your CI to run npm test. Does this solve your problem?

Upvotes: 3

Related Questions