Skip to content

Commit d441d8b

Browse files
byCedricmarionebl
authored andcommitted
refactor(format): expose format result with extra tests (#580)
* refactor(format): expose format result with extra tests * refactor(format): expose format result with extra tests * fix(cli): use the default export in formatters if any
1 parent 7503e34 commit d441d8b

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

@commitlint/cli/src/cli.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const load = require('@commitlint/load');
55
const lint = require('@commitlint/lint');
66
const read = require('@commitlint/read');
77
const meow = require('meow');
8-
const {merge, pick} = require('lodash');
8+
const {merge, pick, isFunction} = require('lodash');
99
const stdin = require('get-stdin');
1010
const resolveFrom = require('resolve-from');
1111
const resolveGlobal = require('resolve-global');
@@ -314,7 +314,13 @@ function loadFormatter(config, flags) {
314314
resolveGlobal.silent(moduleName);
315315

316316
if (modulePath) {
317-
return require(modulePath);
317+
const moduleInstance = require(modulePath);
318+
319+
if (isFunction(moduleInstance.default)) {
320+
return moduleInstance.default;
321+
}
322+
323+
return moduleInstance;
318324
}
319325

320326
throw new Error(`Using format ${moduleName}, but cannot find the module.`);

@commitlint/format/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function formatInput(result = {}, options = {}) {
3838
return `\n${decoration} input: ${decoratedInput}\n`;
3939
}
4040

41-
function formatResult(result = {}, options = {}) {
41+
export function formatResult(result = {}, options = {}) {
4242
const {
4343
signs = DEFAULT_SIGNS,
4444
colors = DEFAULT_COLORS,

@commitlint/format/src/index.test.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import test from 'ava';
22
import chalk from 'chalk';
33
import {includes} from 'lodash';
4-
import format from '.';
4+
import format, {formatResult} from '.';
55

66
const ok = chalk.bold(
77
`${chalk.green(
@@ -144,3 +144,46 @@ test('uses signs as configured', t => {
144144
t.true(includes(actualError, 'ERR'));
145145
t.true(includes(actualWarning, 'WRN'));
146146
});
147+
148+
test('format result provides summary without arguments', t => {
149+
const actual = formatResult();
150+
const actualText = actual.join('\n');
151+
152+
t.true(includes(actualText, '0 problems, 0 warnings'));
153+
});
154+
155+
test('format result transforms error to text', t => {
156+
const actual = formatResult({
157+
errors: [
158+
{
159+
level: 2,
160+
name: 'error-name',
161+
message: 'There was an error'
162+
}
163+
]
164+
});
165+
166+
const actualText = actual.join('\n');
167+
168+
t.true(includes(actualText, 'error-name'));
169+
t.true(includes(actualText, 'There was an error'));
170+
t.true(includes(actualText, '1 problems, 0 warnings'));
171+
});
172+
173+
test('format result transforms warning to text', t => {
174+
const actual = formatResult({
175+
warnings: [
176+
{
177+
level: 1,
178+
name: 'warning-name',
179+
message: 'There was a warning'
180+
}
181+
]
182+
});
183+
184+
const actualText = actual.join('\n');
185+
186+
t.true(includes(actualText, 'warning-name'));
187+
t.true(includes(actualText, 'There was a warning'));
188+
t.true(includes(actualText, '0 problems, 1 warnings'));
189+
});

0 commit comments

Comments
 (0)