11import { describe , expect , test } from "bun:test" ;
2+ import { OTHER_COMMIT_TYPE } from "../../src/constants" ;
23import type { CommitInfo } from "../../src/domain/Commit" ;
3- import { Commit , OTHER_COMMIT_TYPE } from "../../src/domain/Commit" ;
4+ import { Commit } from "../../src/domain/Commit" ;
45
56const HASH = "abc1234567890def" ;
7+ const AUTHOR = "test-author" ;
68
79describe ( "Commit.parse" , ( ) => {
810 test ( "parses standard feat commit" , ( ) => {
9- const commit = Commit . parse ( HASH , "feat: add feature" ) ;
10-
11- expect ( commit . type ) . toBe ( "feat" ) ;
12- expect ( commit . message ) . toBe ( "add feature" ) ;
13- expect ( commit . breaking ) . toBe ( false ) ;
14- expect ( commit . scope ) . toBeUndefined ( ) ;
15- expect ( commit . subject ) . toBe ( "feat: add feature" ) ;
16- expect ( commit . hash ) . toBe ( HASH ) ;
17- expect ( commit . files ) . toEqual ( [ ] ) ;
11+ const commit = Commit . parse ( HASH , "feat: add feature" , AUTHOR ) ;
12+
13+ expect ( commit ) . toMatchObject ( {
14+ breaking : false ,
15+ files : [ ] ,
16+ hash : HASH ,
17+ message : "add feature" ,
18+ scope : undefined ,
19+ subject : "feat: add feature" ,
20+ type : "feat" ,
21+ } ) ;
1822 } ) ;
1923
2024 test ( "parses standard fix commit" , ( ) => {
21- const commit = Commit . parse ( HASH , "fix: resolve bug" ) ;
25+ const commit = Commit . parse ( HASH , "fix: resolve bug" , AUTHOR ) ;
2226
23- expect ( commit . type ) . toBe ( "fix" ) ;
24- expect ( commit . message ) . toBe ( "resolve bug" ) ;
25- expect ( commit . breaking ) . toBe ( false ) ;
27+ expect ( commit ) . toMatchObject ( { breaking : false , message : "resolve bug" , type : "fix" } ) ;
2628 } ) ;
2729
2830 test ( "parses commit with scope" , ( ) => {
29- const commit = Commit . parse ( HASH , "feat(api): add endpoint" ) ;
31+ const commit = Commit . parse ( HASH , "feat(api): add endpoint" , AUTHOR ) ;
3032
31- expect ( commit . type ) . toBe ( "feat" ) ;
32- expect ( commit . scope ) . toBe ( "api" ) ;
33- expect ( commit . message ) . toBe ( "add endpoint" ) ;
34- expect ( commit . breaking ) . toBe ( false ) ;
33+ expect ( commit ) . toMatchObject ( { breaking : false , message : "add endpoint" , scope : "api" , type : "feat" } ) ;
3534 } ) ;
3635
3736 test ( "parses breaking change with !" , ( ) => {
38- const commit = Commit . parse ( HASH , "feat!: breaking change" ) ;
37+ const commit = Commit . parse ( HASH , "feat!: breaking change" , AUTHOR ) ;
3938
40- expect ( commit . type ) . toBe ( "feat" ) ;
41- expect ( commit . breaking ) . toBe ( true ) ;
42- expect ( commit . message ) . toBe ( "breaking change" ) ;
39+ expect ( commit ) . toMatchObject ( { breaking : true , message : "breaking change" , type : "feat" } ) ;
4340 } ) ;
4441
4542 test ( "parses breaking change with scope and !" , ( ) => {
46- const commit = Commit . parse ( HASH , "feat(api)!: break it" ) ;
43+ const commit = Commit . parse ( HASH , "feat(api)!: break it" , AUTHOR ) ;
4744
48- expect ( commit . type ) . toBe ( "feat" ) ;
49- expect ( commit . scope ) . toBe ( "api" ) ;
50- expect ( commit . breaking ) . toBe ( true ) ;
51- expect ( commit . message ) . toBe ( "break it" ) ;
45+ expect ( commit ) . toMatchObject ( { breaking : true , message : "break it" , scope : "api" , type : "feat" } ) ;
5246 } ) ;
5347
5448 describe ( "all known commit types" , ( ) => {
5549 const knownTypes = [ "build" , "chore" , "ci" , "docs" , "feat" , "fix" , "perf" , "refactor" , "style" , "test" ] ;
5650
5751 for ( const type of knownTypes ) {
5852 test ( `recognizes "${ type } " as conventional type` , ( ) => {
59- const commit = Commit . parse ( HASH , `${ type } : some message` ) ;
53+ const commit = Commit . parse ( HASH , `${ type } : some message` , AUTHOR ) ;
6054
61- expect ( commit . type ) . toBe ( type ) ;
62- expect ( commit . message ) . toBe ( "some message" ) ;
63- expect ( commit . isConventional ) . toBe ( true ) ;
55+ expect ( commit ) . toMatchObject ( { isConventional : true , message : "some message" , type } ) ;
6456 } ) ;
6557 }
6658 } ) ;
6759
6860 test ( "unknown type falls back to OTHER_COMMIT_TYPE" , ( ) => {
69- const commit = Commit . parse ( HASH , "unknown: something" ) ;
61+ const commit = Commit . parse ( HASH , "unknown: something" , AUTHOR ) ;
7062
71- expect ( commit . type ) . toBe ( OTHER_COMMIT_TYPE ) ;
72- expect ( commit . type ) . toBe ( "other" ) ;
73- expect ( commit . message ) . toBe ( "unknown: something" ) ;
63+ expect ( commit ) . toMatchObject ( { message : "unknown: something" , type : OTHER_COMMIT_TYPE } ) ;
7464 } ) ;
7565
7666 test ( "non-conventional commit falls back to OTHER_COMMIT_TYPE" , ( ) => {
77- const commit = Commit . parse ( HASH , "just a message" ) ;
67+ const commit = Commit . parse ( HASH , "just a message" , AUTHOR ) ;
7868
79- expect ( commit . type ) . toBe ( OTHER_COMMIT_TYPE ) ;
80- expect ( commit . message ) . toBe ( "just a message" ) ;
81- expect ( commit . subject ) . toBe ( "just a message" ) ;
69+ expect ( commit ) . toMatchObject ( { message : "just a message" , subject : "just a message" , type : OTHER_COMMIT_TYPE } ) ;
8270 } ) ;
8371
8472 test ( "commit with colon but no known type falls back to OTHER_COMMIT_TYPE" , ( ) => {
85- const commit = Commit . parse ( HASH , "WIP: work in progress" ) ;
73+ const commit = Commit . parse ( HASH , "WIP: work in progress" , AUTHOR ) ;
8674
87- expect ( commit . type ) . toBe ( OTHER_COMMIT_TYPE ) ;
88- expect ( commit . message ) . toBe ( "WIP: work in progress" ) ;
75+ expect ( commit ) . toMatchObject ( { message : "WIP: work in progress" , type : OTHER_COMMIT_TYPE } ) ;
8976 } ) ;
9077
9178 test ( "empty subject edge case" , ( ) => {
92- const commit = Commit . parse ( HASH , "" ) ;
79+ const commit = Commit . parse ( HASH , "" , AUTHOR ) ;
9380
94- expect ( commit . type ) . toBe ( OTHER_COMMIT_TYPE ) ;
95- expect ( commit . message ) . toBe ( "" ) ;
96- expect ( commit . subject ) . toBe ( "" ) ;
97- expect ( commit . breaking ) . toBe ( false ) ;
81+ expect ( commit ) . toMatchObject ( { breaking : false , message : "" , subject : "" , type : OTHER_COMMIT_TYPE } ) ;
9882 } ) ;
9983} ) ;
10084
10185describe ( "Commit getters" , ( ) => {
10286 test ( "shortHash returns first 7 characters" , ( ) => {
103- const commit = Commit . parse ( HASH , "feat: something" ) ;
104-
105- expect ( commit . shortHash ) . toBe ( "abc1234" ) ;
106- expect ( commit . shortHash ) . toHaveLength ( 7 ) ;
87+ expect ( Commit . parse ( HASH , "feat: something" , AUTHOR ) . shortHash ) . toBe ( "abc1234" ) ;
10788 } ) ;
10889
10990 test ( "shortHash works with exactly 7 char hash" , ( ) => {
110- const commit = Commit . parse ( "abc1234" , "feat: something" ) ;
111-
112- expect ( commit . shortHash ) . toBe ( "abc1234" ) ;
91+ expect ( Commit . parse ( "abc1234" , "feat: something" , AUTHOR ) . shortHash ) . toBe ( "abc1234" ) ;
11392 } ) ;
11493
11594 test ( "isConventional returns true for known types" , ( ) => {
116- const commit = Commit . parse ( HASH , "feat: something" ) ;
117-
118- expect ( commit . isConventional ) . toBe ( true ) ;
95+ expect ( Commit . parse ( HASH , "feat: something" , AUTHOR ) . isConventional ) . toBe ( true ) ;
11996 } ) ;
12097
12198 test ( "isConventional returns false for other type" , ( ) => {
122- const commit = Commit . parse ( HASH , "random message" ) ;
123-
124- expect ( commit . isConventional ) . toBe ( false ) ;
99+ expect ( Commit . parse ( HASH , "random message" , AUTHOR ) . isConventional ) . toBe ( false ) ;
125100 } ) ;
126101} ) ;
127102
128103describe ( "Commit.withFiles" , ( ) => {
129- test ( "returns new Commit with files set" , ( ) => {
130- const original = Commit . parse ( HASH , "feat: something" ) ;
104+ test ( "returns new Commit with files set, original unchanged " , ( ) => {
105+ const original = Commit . parse ( HASH , "feat: something" , AUTHOR ) ;
131106 const files = [ "src/index.ts" , "src/utils.ts" ] ;
132107 const withFiles = original . withFiles ( files ) ;
133108
134- expect ( withFiles . files ) . toEqual ( files ) ;
135- expect ( withFiles . type ) . toBe ( "feat" ) ;
136- expect ( withFiles . message ) . toBe ( "something" ) ;
137- expect ( withFiles . hash ) . toBe ( HASH ) ;
138- // Original is unchanged
109+ expect ( withFiles ) . toMatchObject ( { files, hash : HASH , message : "something" , type : "feat" } ) ;
139110 expect ( original . files ) . toEqual ( [ ] ) ;
140111 } ) ;
141112} ) ;
142113
143114describe ( "Commit.withBody" , ( ) => {
144- test ( "returns new Commit with body set" , ( ) => {
145- const original = Commit . parse ( HASH , "feat: something" ) ;
115+ test ( "returns new Commit with body set, original unchanged " , ( ) => {
116+ const original = Commit . parse ( HASH , "feat: something" , AUTHOR ) ;
146117 const withBody = original . withBody ( "detailed description" ) ;
147118
148- expect ( withBody . body ) . toBe ( "detailed description" ) ;
149- expect ( withBody . type ) . toBe ( "feat" ) ;
150- expect ( withBody . message ) . toBe ( "something" ) ;
151- // Original is unchanged
119+ expect ( withBody ) . toMatchObject ( { body : "detailed description" , message : "something" , type : "feat" } ) ;
152120 expect ( original . body ) . toBeUndefined ( ) ;
153121 } ) ;
154122
155- test ( "returns new Commit with undefined body" , ( ) => {
156- const original = Commit . parse ( HASH , "feat: something" ) . withBody ( "some body" ) ;
157- const withoutBody = original . withBody ( undefined ) ;
123+ test ( "withBody(undefined) clears the body" , ( ) => {
124+ const original = Commit . parse ( HASH , "feat: something" , AUTHOR ) . withBody ( "some body" ) ;
158125
159- expect ( withoutBody . body ) . toBeUndefined ( ) ;
126+ expect ( original . withBody ( undefined ) . body ) . toBeUndefined ( ) ;
160127 } ) ;
161128} ) ;
162129
163130describe ( "Commit.toInfo" , ( ) => {
164131 test ( "returns CommitInfo with correct fields" , ( ) => {
165- const commit = Commit . parse ( HASH , "feat(api): add endpoint" ) . withBody ( "some body" ) . withFiles ( [ "src/api.ts" ] ) ;
166-
132+ const commit = Commit . parse ( HASH , "feat(api): add endpoint" , AUTHOR )
133+ . withBody ( "some body" )
134+ . withFiles ( [ "src/api.ts" ] ) ;
167135 const info : CommitInfo = commit . toInfo ( [ "@org/api" ] ) ;
168136
169137 expect ( info ) . toEqual ( {
@@ -178,15 +146,16 @@ describe("Commit.toInfo", () => {
178146 } ) ;
179147
180148 test ( "returns CommitInfo for non-conventional commit" , ( ) => {
181- const commit = Commit . parse ( HASH , "just a message" ) ;
182-
183- const info = commit . toInfo ( [ ] ) ;
149+ const info = Commit . parse ( HASH , "just a message" , AUTHOR ) . toInfo ( [ ] ) ;
184150
185- expect ( info . type ) . toBe ( "other" ) ;
186- expect ( info . message ) . toBe ( "just a message" ) ;
187- expect ( info . hash ) . toBe ( "abc1234" ) ;
188- expect ( info . packages ) . toEqual ( [ ] ) ;
189- expect ( info . scope ) . toBeUndefined ( ) ;
190- expect ( info . body ) . toBeUndefined ( ) ;
151+ expect ( info ) . toEqual ( {
152+ body : undefined ,
153+ hash : "abc1234" ,
154+ message : "just a message" ,
155+ packages : [ ] ,
156+ scope : undefined ,
157+ subject : "just a message" ,
158+ type : "other" ,
159+ } ) ;
191160 } ) ;
192161} ) ;
0 commit comments