mardi 5 juillet 2016

Testing yeoman sub-generator

I'm trying to test a generator using the inDir() helper method to run 2 different test scenarios in 2 different directories, and getting confusing results.

The first time the generator is run, everything works as expected. It copies all of the files into my tmp1 directory.

When the generator is run from the second describe block, however, the only files copied are .yo-rc and Gruntfile.js.

I think this has something to do with my test setup, because if I put a .skip on the first describe, then the generator runs correctly in tmp2/.

In the following example, I've used the same configuration for both to reduce the number of variables, but in practice, each generator would be passed a different set of prompts.

Here's my test file:

let prompts = require('../fixtures/prompts.js');

describe('generator,', function () {
  this.timeout(0);

  let generator = require.resolve('../../app/index.js');

  beforeEach(function() {
    let options = {
      skipInstall: true,
    }

    this.generator = helpers.run(require.resolve('../../app/index.js'))
      .withPrompts(prompts.default)
      .withOptions(options)
  });

  describe('in tmp1/', function() {

    beforeEach(function(done) {

      this.generator
        .inDir(path.resolve(__dirname, '../../tmp1'))
        .on('end', done);
    });

    it('should copy the files', function(){
      assert.file('dummyfile.txt');
    });
  });

  describe('in tmp2/', function() {
    beforeEach(function(done) {
      this.generator
        .inDir(path.resolve(__dirname, '../../tmp2'))
        .on('end', done);
    });

    it('should not be crazy', function() {
      assert.file('dummyfile.txt');
    }); 
  });
});

What might be causing this?

Note:

For context, I want to test the scenario of running a sub-generator in a folder that already contains a project created by the base generator, to make sure that it correctly uses the existing project configs. I'm not sure if what I'm doing here is the best way to accomplish that, but I thought this problem might be confusing enough to merit its own question.

Aucun commentaire:

Enregistrer un commentaire