Skip to content
This repository was archived by the owner on Feb 21, 2020. It is now read-only.

Commit a885376

Browse files
author
Charlike Mike Reagent
committed
feat: ensurance tests + allow body to be empty string
Signed-off-by: Charlike Mike Reagent <mameto2011@gmail.com>
1 parent 412d205 commit a885376

File tree

10 files changed

+40
-12
lines changed

10 files changed

+40
-12
lines changed

src/commit.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ export function checkCommit(commit) {
149149

150150
const isValidBody =
151151
'body' in commit && commit.body !== null
152-
? isValidString(commit.body)
152+
? typeof commit.body === 'string'
153153
: true;
154154

155155
if (!isValidBody) {
156-
throw new TypeError('commit.body should be non empty string when given');
156+
throw new TypeError('commit.body should be string when given');
157157
}
158158

159159
const isValid =

test/commit/check.test.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ test('.checkCommit throw if commit.header.scope is not a string when given', (t)
3434

3535
test('.checkCommit throw if commit.body is not a string when given', (t) => {
3636
const commit = { header: { type: 'fix', subject: 'qux zaz' }, body: 123 };
37-
t.throws(
38-
() => checkCommit(commit),
39-
/body should be non empty string when given/,
40-
);
37+
t.throws(() => checkCommit(commit), /body should be string when given/);
4138
});
4239
test('.checkCommit throw if commit.footer is not a string when given', (t) => {
4340
const commit = { header: { type: 'fix', subject: 'qux zaz' }, footer: 123 };
@@ -98,3 +95,11 @@ test('.checkCommit object with scope, body and footer', (t) => {
9895
};
9996
t.deepStrictEqual(checkCommit(commit), commit);
10097
});
98+
99+
test('should checkCommit allow empty string body', (t) => {
100+
const header = { type: 'feat', scope: 'zazzy', subject: 'okey dude' };
101+
const commit = { header, body: '' };
102+
const result = checkCommit(commit);
103+
104+
t.strictEqual(result.body, '');
105+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{".parseCommit throw if not a string given":{"skip":false,"todo":false,"run":false,"isPending":false,"isRejected":false,"isFulfilled":true,"id":1,"str":"(t) => {\n t.throws(() => parseCommit(123), TypeError);\n t.throws(() => parseCommit(123), /expect `commit` to be non empty string/);\n t.throws(() => parseCommit(''), /expect `commit` to be non empty string/);\n}","title":".parseCommit throw if not a string given"},".parseCommit throw when invalid conventional commits":{"skip":false,"todo":false,"run":false,"isPending":false,"isRejected":false,"isFulfilled":true,"id":2,"str":"(t) => {\n function fixture() {\n parseCommit('fix bar qux');\n }\n t.throws(fixture, Error);\n t.throws(fixture, /<type>\\[optional scope\\]: <description>/);\n}","title":".parseCommit throw when invalid conventional commits"},".parseCommit NOT throw when header is valid by conventional commits":{"skip":false,"todo":false,"run":false,"isPending":false,"isRejected":false,"isFulfilled":true,"id":3,"str":"(t) => {\n const one = parseCommit('fix: zzz qux');\n const two = parseCommit('fix(cli): aaaa qux');\n const res = parseCommit('fix(cli): qqqqq qux\\n\\nSome awesome body');\n\n t.ok(isObject(one));\n t.ok(isObject(two));\n t.ok(isObject(res));\n}","title":".parseCommit NOT throw when header is valid by conventional commits"},".parseCommit correctly commit message string without scope":{"skip":false,"todo":false,"run":false,"isPending":false,"isRejected":false,"isFulfilled":true,"id":4,"str":"(t) => {\n const result = parseCommit('fix: bar qux');\n\n t.deepStrictEqual(result, {\n header: { type: 'fix', scope: null, subject: 'bar qux' },\n body: null,\n footer: null,\n });\n}","title":".parseCommit correctly commit message string without scope"},".parseCommit header string with scope":{"skip":false,"todo":false,"run":false,"isPending":false,"isRejected":false,"isFulfilled":true,"id":5,"str":"(t) => {\n t.deepStrictEqual(parseCommit('fix(cli): bar qux'), {\n header: { type: 'fix', scope: 'cli', subject: 'bar qux' },\n body: null,\n footer: null,\n });\n}","title":".parseCommit header string with scope"}}

test/commit/snapshots/validate.test.snapshot.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/header/snapshots/check.test.snapshot.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/header/snapshots/validate.test.snapshot.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/index.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { EOL } from 'os';
12
import test from 'asia';
23
import dedent from 'dedent';
3-
import { applyPlugins, plugins, mappers, parse } from '../src';
4+
import { applyPlugins, plugins, mappers, parse, check } from '../src';
45

56
test('index: basic', (t) => {
67
t.strictEqual(typeof applyPlugins, 'function');
@@ -108,3 +109,27 @@ test('should result.increment be false if no bump needed', (t) => {
108109
t.strictEqual(result.header.scope, 'zz');
109110
t.strictEqual(result.header.subject, 'foo bar');
110111
});
112+
113+
test('ensure most common usage', (t) => {
114+
const commitMsg = 'fix: bar qux';
115+
const res1 = applyPlugins(plugins, check(parse(commitMsg)));
116+
const res2 = applyPlugins(plugins, check(parse(commitMsg + EOL)));
117+
const res3 = applyPlugins(plugins, check(parse(commitMsg + EOL + EOL)));
118+
119+
t.deepStrictEqual(res1, res2);
120+
121+
t.strictEqual(res2[0].body, null);
122+
t.strictEqual(res2[0].header.type, 'fix');
123+
t.strictEqual(res2[0].header.scope, null);
124+
t.strictEqual(res2[0].header.subject, 'bar qux');
125+
t.strictEqual(res2[0].isBreaking, false);
126+
t.strictEqual(res2[0].increment, 'patch');
127+
128+
const [cmt] = res3;
129+
t.strictEqual(cmt.header.type, 'fix');
130+
t.strictEqual(cmt.header.scope, null);
131+
t.strictEqual(cmt.header.subject, 'bar qux');
132+
t.strictEqual(cmt.body.trim(), '');
133+
t.strictEqual(cmt.isBreaking, false);
134+
t.strictEqual(cmt.increment, 'patch');
135+
});

test/main/snapshots/parse.test.snapshot.json

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{".validate should return boolean when second param `ret` is not given":{"skip":false,"todo":false,"run":false,"isPending":false,"isRejected":false,"isFulfilled":true,"id":1,"str":"(t) => {\n const header = { type: 'fix', subject: 'bar qux' };\n const boolTrue = validate({\n header,\n body: 'foo',\n });\n const boolFalse = validate({ header, body: 111, footer: 'sasa' });\n\n t.strictEqual(boolTrue, true);\n t.strictEqual(boolFalse, false);\n}","title":".validate should return boolean when second param `ret` is not given"},".validate should return { error, value } objects when `ret` is true":{"skip":false,"todo":false,"run":false,"isPending":false,"isRejected":false,"isFulfilled":true,"id":2,"str":"(t) => {\n const header = { type: 'major', scope: 'qux', subject: 'some awful change' };\n const result = validate({ header, body: 'okkk' }, true);\n\n t.deepStrictEqual(result.value, [{ header, body: 'okkk', footer: null }]);\n\n const res2 = validate({ header: { foo: 1 }, body: 'a' }, true);\n t.ok(res2.error);\n t.ok(res2.error instanceof Error);\n t.ok(!res2.value);\n}","title":".validate should return { error, value } objects when `ret` is true"},"should validate() similar to check() be able to accept array of mixed values":{"skip":false,"todo":false,"run":false,"isPending":false,"isRejected":false,"isFulfilled":true,"id":3,"str":"(t) => {\n const commits = [\n 'fix: abr\\n\\nquxx qq',\n { header: { type: 'qq', subject: 'ok ok' } },\n ];\n const { value } = validate(commits, true);\n\n t.deepStrictEqual(value, [\n {\n header: { type: 'fix', scope: null, subject: 'abr' },\n body: 'quxx qq',\n footer: null,\n },\n {\n header: { type: 'qq', scope: null, subject: 'ok ok' },\n body: null,\n footer: null,\n },\n ]);\n}","title":"should validate() similar to check() be able to accept array of mixed values"}}

0 commit comments

Comments
 (0)