1
1
/// <reference path="./inquirer/inquirer.d.ts" />
2
2
3
- import { test , expect , vi } from 'vitest' ;
3
+ import { expect , test , vi } from 'vitest' ;
4
4
// @ts -expect-error -- no typings
5
5
import config from '@commitlint/config-angular' ;
6
6
import chalk from 'chalk' ;
7
- import { Answers , DistinctQuestion , PromptModule } from 'inquirer' ;
7
+ import {
8
+ Answers ,
9
+ DistinctQuestion ,
10
+ InputCustomOptions ,
11
+ PromptModule ,
12
+ } from 'inquirer' ;
8
13
9
14
import { input } from './input.js' ;
10
15
16
+ const testConfig = {
17
+ parserPreset : config . parserPreset ,
18
+ rules : {
19
+ ...config . rules ,
20
+ } ,
21
+ } ;
22
+
11
23
vi . mock ( '@commitlint/load' , ( ) => ( {
12
- default : ( ) => config ,
24
+ default : ( ) => testConfig ,
13
25
} ) ) ;
14
26
15
27
test ( 'should work with all fields filled' , async ( ) => {
28
+ const prompt = stub ( {
29
+ 'input-custom' : {
30
+ type : 'fix' ,
31
+ scope : 'test' ,
32
+ subject : 'subject' ,
33
+ body : 'body' ,
34
+ footer : 'footer' ,
35
+ } ,
36
+ } ) ;
37
+ const message = await input ( prompt ) ;
38
+ expect ( message ) . toEqual ( 'fix(test): subject\n' + '\nbody\n' + '\nfooter' ) ;
39
+ } ) ;
40
+
41
+ test ( 'should not add leading blank line to body and footer if rules are disabled' , async ( ) => {
42
+ testConfig . rules [ 'body-leading-blank' ] = [ '1' , 'never' ] ;
43
+ testConfig . rules [ 'footer-leading-blank' ] = [ '1' , 'never' ] ;
16
44
const prompt = stub ( {
17
45
'input-custom' : {
18
46
type : 'fix' ,
@@ -24,6 +52,10 @@ test('should work with all fields filled', async () => {
24
52
} ) ;
25
53
const message = await input ( prompt ) ;
26
54
expect ( message ) . toEqual ( 'fix(test): subject\n' + 'body\n' + 'footer' ) ;
55
+ // reset config mock
56
+ testConfig . rules [ 'body-leading-blank' ] = config . rules [ 'body-leading-blank' ] ;
57
+ testConfig . rules [ 'footer-leading-blank' ] =
58
+ config . rules [ 'footer-leading-blank' ] ;
27
59
} ) ;
28
60
29
61
test ( 'should work without scope' , async ( ) => {
@@ -37,7 +69,7 @@ test('should work without scope', async () => {
37
69
} ,
38
70
} ) ;
39
71
const message = await input ( prompt ) ;
40
- expect ( message ) . toEqual ( 'fix: subject\n' + 'body\ n' + 'footer ' ) ;
72
+ expect ( message ) . toEqual ( 'fix: subject\n' + '\nbody\ n' + '\nfooter ' ) ;
41
73
} ) ;
42
74
43
75
test ( 'should fail without type' , async ( ) => {
@@ -72,7 +104,7 @@ function stub(config: Record<string, Record<string, unknown>>): PromptModule {
72
104
if ( ! questions ) {
73
105
throw new Error ( `Unexpected config type: ${ configType } ` ) ;
74
106
}
75
- const answer = questions [ promptConfig . name ! ] ;
107
+ let answer = questions [ promptConfig . name ! ] ;
76
108
if ( answer == null ) {
77
109
throw new Error ( `Unexpected config name: ${ promptConfig . name } ` ) ;
78
110
}
@@ -83,7 +115,11 @@ function stub(config: Record<string, Record<string, unknown>>): PromptModule {
83
115
throw new Error ( validationResult || undefined ) ;
84
116
}
85
117
}
86
-
118
+ const forceLeadingBlankFn = ( promptConfig as InputCustomOptions )
119
+ . forceLeadingBlankFn ;
120
+ if ( forceLeadingBlankFn ) {
121
+ answer = forceLeadingBlankFn ( answer as string ) ;
122
+ }
87
123
result [ promptConfig . name ! ] = answer ;
88
124
}
89
125
return result ;
0 commit comments