11/// <reference path="./inquirer/inquirer.d.ts" />
22
3- import { test , expect , vi } from 'vitest' ;
3+ import { expect , test , vi } from 'vitest' ;
44// @ts -expect-error -- no typings
55import config from '@commitlint/config-angular' ;
66import chalk from 'chalk' ;
7- import { Answers , DistinctQuestion , PromptModule } from 'inquirer' ;
7+ import {
8+ Answers ,
9+ DistinctQuestion ,
10+ InputCustomOptions ,
11+ PromptModule ,
12+ } from 'inquirer' ;
813
914import { input } from './input.js' ;
1015
16+ const testConfig = {
17+ parserPreset : config . parserPreset ,
18+ rules : {
19+ ...config . rules ,
20+ } ,
21+ } ;
22+
1123vi . mock ( '@commitlint/load' , ( ) => ( {
12- default : ( ) => config ,
24+ default : ( ) => testConfig ,
1325} ) ) ;
1426
1527test ( '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' ] ;
1644 const prompt = stub ( {
1745 'input-custom' : {
1846 type : 'fix' ,
@@ -24,6 +52,10 @@ test('should work with all fields filled', async () => {
2452 } ) ;
2553 const message = await input ( prompt ) ;
2654 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' ] ;
2759} ) ;
2860
2961test ( 'should work without scope' , async ( ) => {
@@ -37,7 +69,7 @@ test('should work without scope', async () => {
3769 } ,
3870 } ) ;
3971 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 ' ) ;
4173} ) ;
4274
4375test ( 'should fail without type' , async ( ) => {
@@ -72,7 +104,7 @@ function stub(config: Record<string, Record<string, unknown>>): PromptModule {
72104 if ( ! questions ) {
73105 throw new Error ( `Unexpected config type: ${ configType } ` ) ;
74106 }
75- const answer = questions [ promptConfig . name ! ] ;
107+ let answer = questions [ promptConfig . name ! ] ;
76108 if ( answer == null ) {
77109 throw new Error ( `Unexpected config name: ${ promptConfig . name } ` ) ;
78110 }
@@ -83,7 +115,11 @@ function stub(config: Record<string, Record<string, unknown>>): PromptModule {
83115 throw new Error ( validationResult || undefined ) ;
84116 }
85117 }
86-
118+ const forceLeadingBlankFn = ( promptConfig as InputCustomOptions )
119+ . forceLeadingBlankFn ;
120+ if ( forceLeadingBlankFn ) {
121+ answer = forceLeadingBlankFn ( answer as string ) ;
122+ }
87123 result [ promptConfig . name ! ] = answer ;
88124 }
89125 return result ;
0 commit comments