Skip to content

Commit 2f9561f

Browse files
committed
test: add jest unit tests, prettier w/ config and .editorconfig
jest unit tests are implemented in engine.test.js and have achieve 100% code coverage and all tests are passing. also add prettier and .editorconfig and reformatted files
1 parent f3b87d5 commit 2f9561f

File tree

8 files changed

+5922
-1815
lines changed

8 files changed

+5922
-1815
lines changed

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[*]
2+
end_of_line = lf
3+
charset = utf-8
4+
indent_size = 2
5+
indent_style = space
6+
insert_final_newline = true
7+
max_line_length = 80
8+
trim_trailing_whitespace = true

.prettierrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"printWidth": 80,
3+
"endOfLine": "lf",
4+
"singleQuote": true
5+
}

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ node_js:
44
- 10
55
- 8
66
- 6
7+
- 4
78

89
jobs:
910
include:

engine.js

Lines changed: 72 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
"format cjs";
1+
'format cjs';
22

33
var wrap = require('word-wrap');
44
var map = require('lodash.map');
55
var longest = require('longest');
66
var rightPad = require('right-pad');
7-
var chalk = require('chalk')
7+
var chalk = require('chalk');
88

99
var filter = function(array) {
1010
return array.filter(function(x) {
@@ -13,39 +13,41 @@ var filter = function(array) {
1313
};
1414

1515
var headerLength = function(answers) {
16-
return answers.type.length + 2 + (answers.scope ? answers.scope.length + 2 : 0)
17-
}
16+
return (
17+
answers.type.length + 2 + (answers.scope ? answers.scope.length + 2 : 0)
18+
);
19+
};
1820

1921
var maxSummaryLength = function(options, answers) {
20-
return options.maxHeaderWidth - headerLength(answers)
21-
}
22+
return options.maxHeaderWidth - headerLength(answers);
23+
};
24+
2225
var filterSubject = function(subject) {
2326
subject = subject.trim();
2427
if (subject.charAt(0).toLowerCase() !== subject.charAt(0)) {
25-
subject = subject.charAt(0).toLowerCase() + subject.slice(1, subject.length);
28+
subject =
29+
subject.charAt(0).toLowerCase() + subject.slice(1, subject.length);
2630
}
2731
while (subject.endsWith('.')) {
28-
subject = subject.slice(0, subject.length - 1)
32+
subject = subject.slice(0, subject.length - 1);
2933
}
30-
return subject
31-
}
34+
return subject;
35+
};
3236

3337
// This can be any kind of SystemJS compatible module.
3438
// We use Commonjs here, but ES6 or AMD would do just
3539
// fine.
36-
module.exports = function (options) {
37-
40+
module.exports = function(options) {
3841
var types = options.types;
3942

4043
var length = longest(Object.keys(types)).length + 1;
41-
var choices = map(types, function (type, key) {
44+
var choices = map(types, function(type, key) {
4245
return {
4346
name: rightPad(key + ':', length) + ' ' + type.description,
4447
value: key
4548
};
4649
});
4750

48-
4951
return {
5052
// When a user runs `git cz`, prompter will
5153
// be executed. We pass you cz, which currently
@@ -59,7 +61,13 @@ module.exports = function (options) {
5961
// By default, we'll de-indent your commit
6062
// template and will keep empty lines.
6163
prompter: function(cz, commit) {
62-
console.log('\nLine 1 will have the maximum length of ' + options.maxHeaderWidth + ' characters (enforced). All other lines will be wrapped after ' + options.maxLineWidth + ' characters.\n');
64+
console.log(
65+
'\nLine 1 will have the maximum length of ' +
66+
options.maxHeaderWidth +
67+
' characters (enforced). All other lines will be wrapped after ' +
68+
options.maxLineWidth +
69+
' characters.\n'
70+
);
6371

6472
// Let's ask some questions of the user
6573
// so that we can populate our commit
@@ -72,61 +80,83 @@ module.exports = function (options) {
7280
{
7381
type: 'list',
7482
name: 'type',
75-
message: 'Select the type of change that you\'re committing:',
83+
message: "Select the type of change that you're committing:",
7684
choices: choices,
7785
default: options.defaultType
78-
}, {
86+
},
87+
{
7988
type: 'input',
8089
name: 'scope',
81-
message: 'What is the scope of this change (e.g. component or file name): (press enter to skip)',
90+
message:
91+
'What is the scope of this change (e.g. component or file name): (press enter to skip)',
8292
default: options.defaultScope,
8393
filter: function(value) {
8494
return value.trim().toLowerCase();
8595
}
86-
}, {
96+
},
97+
{
8798
type: 'input',
8899
name: 'subject',
89-
message: function (answers) {
90-
return 'Write a short, imperative tense description of the change (max ' + maxSummaryLength(options, answers) + ' chars):\n'
100+
message: function(answers) {
101+
return (
102+
'Write a short, imperative tense description of the change (max ' +
103+
maxSummaryLength(options, answers) +
104+
' chars):\n'
105+
);
91106
},
92107
default: options.defaultSubject,
93108
validate: function(subject, answers) {
94-
var filteredSubject = filterSubject(subject)
95-
return filteredSubject.length == 0 ? 'Subject is required' :
96-
filteredSubject.length <= maxSummaryLength(options, answers) ? true :
97-
'Subject length must be less than or equal to ' + maxSummaryLength(options, answers) + ' characters. Current length is ' + filteredSubject.length + ' characters.';
109+
var filteredSubject = filterSubject(subject);
110+
return filteredSubject.length == 0
111+
? 'subject is required'
112+
: filteredSubject.length <= maxSummaryLength(options, answers)
113+
? true
114+
: 'Subject length must be less than or equal to ' +
115+
maxSummaryLength(options, answers) +
116+
' characters. Current length is ' +
117+
filteredSubject.length +
118+
' characters.';
98119
},
99120
transformer: function(subject, answers) {
100-
var filteredSubject = filterSubject(subject)
101-
var color = filteredSubject.length <= maxSummaryLength(options, answers) ? chalk.green : chalk.red
102-
return color('(' + filteredSubject.length + ') ' + subject)
121+
var filteredSubject = filterSubject(subject);
122+
var color =
123+
filteredSubject.length <= maxSummaryLength(options, answers)
124+
? chalk.green
125+
: chalk.red;
126+
return color('(' + filteredSubject.length + ') ' + subject);
103127
},
104128
filter: function(subject) {
105-
return filterSubject(subject)
129+
return filterSubject(subject);
106130
}
107-
}, {
131+
},
132+
{
108133
type: 'input',
109134
name: 'body',
110-
message: 'Provide a longer description of the change: (press enter to skip)\n',
135+
message:
136+
'Provide a longer description of the change: (press enter to skip)\n',
111137
default: options.defaultBody
112-
}, {
138+
},
139+
{
113140
type: 'confirm',
114141
name: 'isBreaking',
115142
message: 'Are there any breaking changes?',
116143
default: false
117-
}, {
144+
},
145+
{
118146
type: 'input',
119147
name: 'breaking',
120148
message: 'Describe the breaking changes:\n',
121149
when: function(answers) {
122150
return answers.isBreaking;
123151
}
124-
}, {
152+
},
153+
{
125154
type: 'confirm',
126155
name: 'isIssueAffected',
127156
message: 'Does this change affect any open issues?',
128157
default: options.defaultIssues ? true : false
129-
}, {
158+
},
159+
{
130160
type: 'input',
131161
name: 'issues',
132162
message: 'Add issue references (e.g. "fix #123", "re #123".):\n',
@@ -136,11 +166,11 @@ module.exports = function (options) {
136166
default: options.defaultIssues ? options.defaultIssues : undefined
137167
}
138168
]).then(function(answers) {
139-
140169
var wrapOptions = {
141170
trim: true,
171+
cut: true,
142172
newline: '\n',
143-
indent:'',
173+
indent: '',
144174
width: options.maxLineWidth
145175
};
146176

@@ -150,17 +180,19 @@ module.exports = function (options) {
150180
// Hard limit this line in the validate
151181
var head = answers.type + scope + ': ' + answers.subject;
152182

153-
// Wrap these lines at 72 characters
183+
// Wrap these lines at options.maxLineWidth characters
154184
var body = answers.body ? wrap(answers.body, wrapOptions) : false;
155185

156186
// Apply breaking change prefix, removing it if already present
157187
var breaking = answers.breaking ? answers.breaking.trim() : '';
158-
breaking = breaking ? 'BREAKING CHANGE: ' + breaking.replace(/^BREAKING CHANGE: /, '') : '';
188+
breaking = breaking
189+
? 'BREAKING CHANGE: ' + breaking.replace(/^BREAKING CHANGE: /, '')
190+
: '';
159191
breaking = breaking ? wrap(breaking, wrapOptions) : false;
160192

161193
var issues = answers.issues ? wrap(answers.issues, wrapOptions) : false;
162194

163-
commit(filter([ head, body, breaking, issues ]).join('\n\n'));
195+
commit(filter([head, body, breaking, issues]).join('\n\n'));
164196
});
165197
}
166198
};

0 commit comments

Comments
 (0)