Skip to content

Commit 9ba4126

Browse files
committed
test(@modulify/conventional-git): Added tests for parser
1 parent afddaca commit 9ba4126

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

packages/conventional-git/tests/parse.test.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,119 @@ describe('parse', () => {
160160
meta: {},
161161
})
162162
})
163+
164+
it('should parse mentions', () => {
165+
const commit = parse('feat: subject\n\nBody with @user1 and @user2')
166+
167+
expect(commit.mentions).toEqual(['user1', 'user2'])
168+
})
169+
170+
it('should parse complex references', () => {
171+
const commit = parse('fix: issue in another repo owner/repo#123')
172+
173+
expect(commit.references).toEqual([{
174+
raw: 'fix: issue in another repo owner/repo#123',
175+
issue: '123',
176+
action: null,
177+
prefix: '#',
178+
owner: 'owner',
179+
repository: 'repo',
180+
}])
181+
})
182+
183+
it('should parse revert commits', () => {
184+
const commit = parse('Revert "feat: original subject"\n\nThis reverts commit 1234567.')
185+
186+
expect(commit.revert).toEqual({
187+
header: 'feat: original subject',
188+
hash: '1234567',
189+
})
190+
})
191+
})
192+
193+
describe('options', () => {
194+
it('should support mergePattern with manageable fields', () => {
195+
const parse = createParser({
196+
mergePattern: /^Merge branch '([\w-]+)' into ([\w-]+)/,
197+
mergeCorrespondence: ['branch', 'body'],
198+
})
199+
200+
const commit = parse('Merge branch \'feature-branch\' into main\n\nfeat: some feature')
201+
202+
expect(commit.merge).toBe('Merge branch \'feature-branch\' into main')
203+
expect(commit.meta.branch).toBe('feature-branch')
204+
expect(commit.body).toContain('main')
205+
})
206+
207+
it('should support custom fieldPattern with manageable fields', () => {
208+
const parse = createParser({ fieldPattern: /^-(.*?)-$/ })
209+
const commit = parse('feat: subject\n\n-body-\nCustom body')
210+
211+
expect(commit.body).toBe('Custom body')
212+
})
213+
214+
it('should support commentChar', () => {
215+
const parse = createParser({ commentChar: '#' })
216+
const commit = parse('feat: subject\n# this is a comment\n\nbody')
217+
218+
expect(commit.body).toBe('body')
219+
})
220+
221+
it('should support custom issuePrefixes', () => {
222+
const parse = createParser({ issuePrefixes: ['GH-'] })
223+
const commit = parse('fix: some bug GH-123')
224+
225+
expect(commit.references[0].issue).toBe('123')
226+
expect(commit.references[0].prefix).toBe('GH-')
227+
})
228+
229+
it('should support multiple notes keywords', () => {
230+
const parse = createParser({
231+
notesKeywords: ['BREAKING CHANGE', 'SECURITY'],
232+
})
233+
234+
const commit = parse('feat: subject\n\nSECURITY: this is a security note')
235+
236+
expect(commit.notes[0]).toEqual({
237+
title: 'SECURITY',
238+
text: 'this is a security note',
239+
})
240+
})
241+
})
242+
243+
describe('edge cases', () => {
244+
const parse = createParser()
245+
246+
it('should handle empty body and footer', () => {
247+
const commit = parse('feat: subject\n\n\n')
248+
249+
expect(commit.body).toBe(null)
250+
expect(commit.footer).toBe(null)
251+
})
252+
253+
it('should handle multiline notes', () => {
254+
const commit = parse('feat: subject\n\nBREAKING CHANGE: line 1\nline 2')
255+
256+
expect(commit.notes[0].text).toBe('line 1\nline 2')
257+
})
258+
259+
it('should handle notes with fields', () => {
260+
const commit = parse('feat: subject\n\nBREAKING CHANGE: some note\n-field-\nvalue')
261+
262+
expect(commit.notes[0].text).toBe('some note')
263+
expect(commit.fields.field).toBe('value')
264+
})
265+
266+
it('should ignore URLs in references', () => {
267+
const commit = parse('fix: see https://example.com/issue/1')
268+
269+
expect(commit.references).toEqual([])
270+
})
271+
272+
it('should handle only line breaks', () => {
273+
const commit = parse('\n\n\n')
274+
275+
expect(commit.header).toBe(null)
276+
})
163277
})
164278
})

0 commit comments

Comments
 (0)