I'm writing unit tests for my yeoman generator. I've noticed that mockPrompt does not use default values for my prompt values. For instance, I have a version parameter that defaults to 0.0.1, but unless I specify a value for version in my test, the result is an empty string. Is this by design or am I doing something wrong.
This is my prompt function from index.js:
var prompts = [{
name: 'libName',
message: 'What do you want to call your lib?'
},{
name: 'libVersion',
message: 'What version would you like to set?',
default: "0.0.1"
},{
name: 'libDesc',
message: 'Describe your lib:'
},{
name: 'authorName',
message: 'What is your full name?'
},{
name: 'angularVersion',
message: 'Enter angular version:',
default: '1.2.7'
},{
name: 'ngResourceRequired',
message: 'Do you require ngResource module?',
type:'confirm',
default: false
}];
this.prompt(prompts, function (props) {
this.libName = props.libName;
this.libVersion = props.libVersion;
this.libDesc = props.libDesc;
this.authorName = props.authorName;
this.angularVersion = props.angularVersion;
this.ngResourceRequired = props.ngResourceRequired;
cb();
}.bind(this));
And this is my test code:
describe('my-jslib:app', function () {
var jslib;
var appPath = 'customAppPath';
var expected = [
appPath + '/.htaccess',
appPath + '/404.html',
appPath + '/favicon.ico',
appPath + '/robots.txt',
appPath + '/styles/main.scss',
appPath + '/views/main.html',
appPath + '/index.html',
'.bowerrc',
'.editorconfig',
'.gitignore',
'.jshintrc',
'Gruntfile.js',
'package.json',
'bower.json'
];
var mockPrompts = {
libName: "test",
//libVersion: "0.0.1",
angularVersion: "1.2.7",
ngResourceRequired: false
};
var genOptions = {
'appPath': appPath,
'skip-install': true,
'skip-welcome-message': true,
'skip-message': true
};
beforeEach(function (done) {
helpers.testDirectory(path.join(__dirname, 'tmp'), function (err) {
if (err) {
done(err);
}
jslib = helpers.createGenerator(
'my-jslib:app', [
'../../app', [
helpers.createDummyGenerator(), 'mocha:app'
]
],
false,
genOptions
);
helpers.mockPrompt(jslib, mockPrompts);
done();
});
});
it('creates expected files', function (done) {
var expected = [
'.bowerrc',
'.editorconfig',
'.gitignore',
'.gitattributes',
'.jshintrc',
'bower.json',
'Gruntfile.js',
'package.json',
'README.md',
'src/' + mockPrompts.libName + '.js',
'test/spec/' + mockPrompts.libName + '.js',
'test/.jshintrcs',
'test/karma.conf.js'
];
jslib.run({}, function () {
helpers.assertFile(expected);
helpers.assertFileContent('package.json', new RegExp('"name": "' + mockPrompts.libName + '"'));
helpers.assertFileContent('bower.json', new RegExp('"name": "' + mockPrompts.libName + '"'));
helpers.assertFileContent('bower.json', new RegExp('"angular": "' + mockPrompts.angularVersion + '"'));
helpers.assertNoFileContent('bower.json', new RegExp('"angular-resource": "' + mockPrompts.angularVersion + '"'));
done();
});
});
});
Thanks, Lior
To mock the default prompt you have to omit the variable in the object of the mock.
var mockPrompts = {
libName: "test",
angularVersion: "1.2.7",
ngResourceRequired: false
};
This will trigger the default value for "libVersion"