diff --git a/src/utils/format-message/formatGitMessage.test.ts b/src/utils/format-message/formatGitMessage.test.ts index d4ffba60..e68eeaf0 100644 --- a/src/utils/format-message/formatGitMessage.test.ts +++ b/src/utils/format-message/formatGitMessage.test.ts @@ -1,5 +1,5 @@ /* eslint-disable jest/no-large-snapshots */ -import { formatCommitMessage } from './formatGitMessage' +import { formatCommitMessage, wrap } from './formatGitMessage' import { defaultConfig } from '../../defaults' const setupFormatCommitMessage = (config = {}, answers = {}): string => { @@ -163,4 +163,52 @@ describe('formatCommitMessage', () => { `"feat(*): ✨ this has \\\\\`quotes\\\\\`"` ) }) + it('should not wrap message when headerMaxLength is longer than the default width (72)', () => { + const formattedMessage = setupFormatCommitMessage( + { ...defaultConfig, headerMaxLength: 75 }, + { + body: '', + breaking: '', + issues: '', + subject: + 'this is a very very very very very very very very very subject', + } + ) + + expect(formattedMessage).toMatchInlineSnapshot( + `"feat(*): ✨ this is a very very very very very very very very very subject"` + ) + expect(formattedMessage).toHaveLength(73) + }) + it('should leverage default width (72) when headerMaxLength is less', () => { + const formattedMessage = setupFormatCommitMessage( + { ...defaultConfig, headerMaxLength: 71 }, + { + body: '', + breaking: '', + issues: '', + subject: + 'this is a very very very very very very very very very subject', // This is intentionally longer than headerMaxLength + } + ) + + expect(formattedMessage).toMatchInlineSnapshot(` +"feat(*): ✨ this is a very very very very very very very very very +subject" +`) + expect(formattedMessage.split('\n')[0]).toHaveLength(65) + }) + describe('wrap', () => { + it('should wrap', () => { + const wrappedString = wrap( + `feat(*): ✨ this is a very very very very very very very very very subject` + ) + + expect(wrappedString).toMatchInlineSnapshot(` +"feat(*): ✨ this is a very very very very very very very very very +subject" +`) + expect(wrappedString.split('\n')[0]).toHaveLength(65) + }) + }) }) diff --git a/src/utils/format-message/formatGitMessage.ts b/src/utils/format-message/formatGitMessage.ts index 2ae80ac6..d15b5638 100644 --- a/src/utils/format-message/formatGitMessage.ts +++ b/src/utils/format-message/formatGitMessage.ts @@ -1,8 +1,10 @@ import { GitzyConfig, Answers } from '../../interfaces' -const wrap = (string: string, width = 72): string => { +const MAX_WIDTH = 72 + +export const wrap = (string: string, maxWidth = MAX_WIDTH): string => { const regex = new RegExp( - `(?![^\\n]{1,${width}}$)([^\\n]{1,${width}})\\s`, + `(?![^\\n]{1,${maxWidth}}$)([^\\n]{1,${maxWidth}})\\s`, 'g' ) @@ -50,6 +52,8 @@ export const formatCommitMessage = ( const body = answers.body.trim() ? `\n\n${answers.body}` : '' const breaking = createBreaking(answers.breaking, config) const issues = createIssues(answers.issues, config) + const maxWidth = + config.headerMaxLength > MAX_WIDTH ? config.headerMaxLength : MAX_WIDTH - return wrap(normalizeMessage(`${head}${body}${breaking}${issues}`)) + return wrap(normalizeMessage(`${head}${body}${breaking}${issues}`), maxWidth) }