Testing the Yeoman Generator with bowerInstall and / or npmInstall

I have a Jomana generator that uses this.bowerInstall()

When I test it, it tries to install all the dependencies that I initialized in this way. Is there a way to mock this feature?

The same goes for the function this.npmInstall().

+4
source share
2 answers

Look at the tests for the bootstrap generator , it contains an example of a bullying function bowerInstall():

beforeEach(function (done) {
    this.bowerInstallCalls = [];

    // Mock bower install and track the function calls.
    this.app.bowerInstall = function () {
        this.bowerInstallCalls.push(arguments);
    }.bind(this);

}.bind(this));
+2
source

. drorb , . RunContext ( Yeoman ( ) [http://yeoman.io/authoring/testing.html]), before .

before(function (done) {
  helpers.run(path.join( __dirname, '../app'))
    .inDir(path.join( __dirname, './tmp'))  // Clear the directory and set it as the CWD
    .withOptions({ foo: 'bar' })            // Mock options passed in
    .withArguments(['name-x'])              // Mock the arguments
    .withPrompt({ coffee: false })          // Mock the prompt answers
    .on('ready', function (generator) {
      // this is called right before `generator.run()`
    })
    .on('end', done);
})

mock- 'ready', :

.on('ready', function(generator) {
  generator.bowerInstall = function(args) {
    // Do something when generator runs bower install
  };
})

- . :

installAngular: function() {
  if (!this.options['skip-install']) {
    this.bowerInstall('angular', {
      'save': true
    });
  }
}

finalInstall: function() {
  this.installDependencies({
    skipInstall: this.options['skip-install']
  });
}

, "skip-install", . , skip-install , . , skip-install, bowerInstall npmInstall , installDependencies ( , )

+3

Source: https://habr.com/ru/post/1570776/


All Articles