@@ -214,6 +214,66 @@ test("should succeed for input from stdin with rules", async () => {
214214 expect ( result . exitCode ) . toBe ( ExitCode . CommitlintDefault ) ;
215215} ) ;
216216
217+ test ( "should fail without config file and without --default-config flag" , async ( ) => {
218+ const cwd = await gitBootstrap ( "fixtures/no-config" ) ;
219+ const result = cli ( [ ] , { cwd } ) ( "feat: this should not work" ) ;
220+ const output = await result ;
221+ expect ( output . stdout . trim ( ) ) . toContain ( "Please add rules" ) ;
222+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintInvalidArgument ) ;
223+ } ) ;
224+
225+ test ( "should succeed for conventional input from stdin without config file with --default-config flag" , async ( ) => {
226+ const cwd = await gitBootstrap ( "fixtures/no-config" ) ;
227+ const result = cli ( [ "--default-config" ] , { cwd } ) ( "feat: this should work" ) ;
228+ const output = await result ;
229+ expect ( output . stderr ) . toEqual ( "" ) ;
230+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintDefault ) ;
231+ } ) ;
232+
233+ test ( "should fail for non-conventional input from stdin without config file with --default-config flag" , async ( ) => {
234+ const cwd = await gitBootstrap ( "fixtures/no-config" ) ;
235+ const result = cli ( [ "--default-config" ] , { cwd } ) ( "this is not a conventional commit" ) ;
236+ const output = await result ;
237+ expect ( output . stdout . trim ( ) ) . toContain ( "type may not be empty" ) ;
238+ expect ( output . stdout . trim ( ) ) . toContain ( "subject may not be empty" ) ;
239+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintErrorDefault ) ;
240+ } ) ;
241+
242+ test ( "should prefer config file over --default-config flag" , async ( ) => {
243+ const cwd = await gitBootstrap ( "fixtures/default" ) ;
244+ // "type: bar" passes the fixture config (type-enum never [foo]) but would
245+ // fail @commitlint /config-conventional (type "type" is not allowed there)
246+ const result = cli ( [ "--default-config" ] , { cwd } ) ( "type: bar" ) ;
247+ await result ;
248+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintDefault ) ;
249+ } ) ;
250+
251+ test ( "should fall back to default config with --default-config flag for a config file without rules" , async ( ) => {
252+ const cwd = await gitBootstrap ( "fixtures/empty" ) ;
253+ const result = cli ( [ "--default-config" ] , { cwd } ) ( "feat: this should work" ) ;
254+ await result ;
255+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintDefault ) ;
256+ } ) ;
257+
258+ test ( "should keep --extends configs when the --default-config fallback applies" , async ( ) => {
259+ const cwd = await gitBootstrap ( "fixtures/no-config" ) ;
260+ // ./helpurl-only contributes a helpUrl but no rules, so the fallback
261+ // still applies and must not drop the user-supplied extends
262+ const result = cli ( [ "--default-config" , "--extends" , "./helpurl-only" ] , { cwd } ) ( "foo bar" ) ;
263+ const output = await result ;
264+ expect ( output . stdout . trim ( ) ) . toContain ( "type may not be empty" ) ;
265+ expect ( output . stdout . trim ( ) ) . toContain ( "https://example.com/no-rules" ) ;
266+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintErrorDefault ) ;
267+ } ) ;
268+
269+ test ( "should point to the --default-config flag when no rules are found" , async ( ) => {
270+ const cwd = await gitBootstrap ( "fixtures/empty" ) ;
271+ const result = cli ( [ ] , { cwd } ) ( "foo: bar" ) ;
272+ const output = await result ;
273+ expect ( output . stdout . trim ( ) ) . toContain ( "--default-config" ) ;
274+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintInvalidArgument ) ;
275+ } ) ;
276+
217277test ( "should fail for input from stdin with rule from rc" , async ( ) => {
218278 const cwd = await gitBootstrap ( "fixtures/simple" ) ;
219279 const result = cli ( [ ] , { cwd } ) ( "foo: bar" ) ;
@@ -609,28 +669,29 @@ test("should print help", async () => {
609669 [input] reads from stdin if --edit, --env, --from and --to are omitted
610670
611671 Options:
612- -c, --color toggle colored output [boolean] [default: true]
613- -g, --config path to the config file; result code 9 if config is missing [string]
614- --print-config print resolved config [string] [choices: "", "text", "json"]
615- -d, --cwd directory to execute in [string] [default: (Working Directory)]
616- -e, --edit read last commit message from the specified file or fallbacks to ./.git/COMMIT_EDITMSG [string]
617- -E, --env check message in the file at path given by environment variable value [string]
618- -x, --extends array of shareable configurations to extend [array]
619- -H, --help-url help url in error message [string]
620- -f, --from lower end of the commit range to lint; applies if edit=false [string]
621- --from-last-tag uses the last tag as the lower end of the commit range to lint; applies if edit=false and from is not set [boolean]
622- --git-log-args additional git log arguments as space separated string, example '--first-parent --cherry-pick' [string]
623- -l, --last just analyze the last commit; applies if edit=false [boolean]
624- -o, --format output format of the results [string]
625- -p, --parser-preset configuration preset to use for conventional-commits-parser [string]
626- -q, --quiet toggle console output [boolean] [default: false]
627- -t, --to upper end of the commit range to lint; applies if edit=false [string]
628- -V, --verbose enable verbose output for reports without problems [boolean]
629- --legacy-output use the legacy input output format (single-line 'input: ...') [boolean]
630- -s, --strict enable strict mode; result code 2 for warnings, 3 for errors [boolean]
631- --options path to a JSON file or Common.js module containing CLI options
632- -v, --version display version information [boolean]
633- -h, --help Show help [boolean]"
672+ -c, --color toggle colored output [boolean] [default: true]
673+ -g, --config path to the config file; result code 9 if config is missing [string]
674+ --default-config use built-in default config (@commitlint/config-conventional) when no rules are found [boolean]
675+ --print-config print resolved config [string] [choices: "", "text", "json"]
676+ -d, --cwd directory to execute in [string] [default: (Working Directory)]
677+ -e, --edit read last commit message from the specified file or fallbacks to ./.git/COMMIT_EDITMSG [string]
678+ -E, --env check message in the file at path given by environment variable value [string]
679+ -x, --extends array of shareable configurations to extend [array]
680+ -H, --help-url help url in error message [string]
681+ -f, --from lower end of the commit range to lint; applies if edit=false [string]
682+ --from-last-tag uses the last tag as the lower end of the commit range to lint; applies if edit=false and from is not set [boolean]
683+ --git-log-args additional git log arguments as space separated string, example '--first-parent --cherry-pick' [string]
684+ -l, --last just analyze the last commit; applies if edit=false [boolean]
685+ -o, --format output format of the results [string]
686+ -p, --parser-preset configuration preset to use for conventional-commits-parser [string]
687+ -q, --quiet toggle console output [boolean] [default: false]
688+ -t, --to upper end of the commit range to lint; applies if edit=false [string]
689+ -V, --verbose enable verbose output for reports without problems [boolean]
690+ --legacy-output use the legacy input output format (single-line 'input: ...') [boolean]
691+ -s, --strict enable strict mode; result code 2 for warnings, 3 for errors [boolean]
692+ --options path to a JSON file or Common.js module containing CLI options
693+ -v, --version display version information [boolean]
694+ -h, --help Show help [boolean]"
634695 ` ) ;
635696} ) ;
636697
@@ -698,6 +759,15 @@ describe("should print config", () => {
698759 `"{"extends":[],"formatter":"@commitlint/format","plugins":{},"rules":{"type-enum":[2,"never",["foo"]]},"helpUrl":"https://github.com/conventional-changelog/commitlint/#what-is-commitlint","prompt":{}}"` ,
699760 ) ;
700761 } ) ;
762+
763+ test ( "should print default config with --default-config flag when no config file is found" , async ( ) => {
764+ const cwd = await gitBootstrap ( "fixtures/no-config" ) ;
765+ const result = cli ( [ "--print-config=json" , "--no-color" , "--default-config" ] , { cwd } ) ( ) ;
766+ const output = await result ;
767+ const printed = JSON . parse ( output . stdout . trim ( ) ) ;
768+ expect ( printed . rules ) . toHaveProperty ( "type-enum" ) ;
769+ expect ( printed . rules [ "type-enum" ] [ 2 ] ) . toContain ( "feat" ) ;
770+ } ) ;
701771} ) ;
702772
703773async function writePkg ( payload : unknown , options : TestOptions ) {
0 commit comments