@@ -27,6 +27,8 @@ const proxyquire = require("proxyquire").noPreserveCache();
2727//------------------------------------------------------------------------------
2828
2929let answers = { } ;
30+ let pkgJSONContents = { } ;
31+ let pkgJSONPath = "" ;
3032
3133describe ( "configInitializer" , ( ) => {
3234
@@ -455,4 +457,121 @@ describe("configInitializer", () => {
455457 } ) ;
456458 } ) ;
457459 } ) ;
460+
461+ describe ( "writeFile()" , ( ) => {
462+
463+ beforeEach ( ( ) => {
464+ answers = {
465+ purpose : "style" ,
466+ source : "prompt" ,
467+ extendDefault : true ,
468+ indent : 2 ,
469+ quotes : "single" ,
470+ linebreak : "unix" ,
471+ semi : true ,
472+ moduleType : "esm" ,
473+ es6Globals : true ,
474+ env : [ "browser" ] ,
475+ format : "JSON"
476+ } ;
477+
478+ pkgJSONContents = {
479+ name : "config-initializer" ,
480+ version : "1.0.0"
481+ } ;
482+
483+ process . chdir ( fixtureDir ) ;
484+
485+ pkgJSONPath = path . resolve ( fixtureDir , "package.json" ) ;
486+ } ) ;
487+
488+ afterEach ( ( ) => {
489+ process . chdir ( originalDir ) ;
490+ } ) ;
491+
492+ it ( "should create .eslintrc.json" , ( ) => {
493+ const config = init . processAnswers ( answers ) ;
494+ const filePath = path . resolve ( fixtureDir , ".eslintrc.json" ) ;
495+
496+ fs . writeFileSync ( pkgJSONPath , JSON . stringify ( pkgJSONContents ) ) ;
497+
498+ init . writeFile ( config , answers . format ) ;
499+
500+ assert . isTrue ( fs . existsSync ( filePath ) ) ;
501+
502+ fs . unlinkSync ( filePath ) ;
503+ fs . unlinkSync ( pkgJSONPath ) ;
504+ } ) ;
505+
506+ it ( "should create .eslintrc.js" , ( ) => {
507+ answers . format = "JavaScript" ;
508+
509+ const config = init . processAnswers ( answers ) ;
510+ const filePath = path . resolve ( fixtureDir , ".eslintrc.js" ) ;
511+
512+ fs . writeFileSync ( pkgJSONPath , JSON . stringify ( pkgJSONContents ) ) ;
513+
514+ init . writeFile ( config , answers . format ) ;
515+
516+ assert . isTrue ( fs . existsSync ( filePath ) ) ;
517+
518+ fs . unlinkSync ( filePath ) ;
519+ fs . unlinkSync ( pkgJSONPath ) ;
520+ } ) ;
521+
522+ it ( "should create .eslintrc.yml" , ( ) => {
523+ answers . format = "YAML" ;
524+
525+ const config = init . processAnswers ( answers ) ;
526+ const filePath = path . resolve ( fixtureDir , ".eslintrc.yml" ) ;
527+
528+ fs . writeFileSync ( pkgJSONPath , JSON . stringify ( pkgJSONContents ) ) ;
529+
530+ init . writeFile ( config , answers . format ) ;
531+
532+ assert . isTrue ( fs . existsSync ( filePath ) ) ;
533+
534+ fs . unlinkSync ( filePath ) ;
535+ fs . unlinkSync ( pkgJSONPath ) ;
536+ } ) ;
537+
538+ // For https://github.com/eslint/eslint/issues/14137
539+ it ( "should create .eslintrc.cjs" , ( ) => {
540+ answers . format = "JavaScript" ;
541+
542+ // create package.json with "type": "module"
543+ pkgJSONContents . type = "module" ;
544+
545+ fs . writeFileSync ( pkgJSONPath , JSON . stringify ( pkgJSONContents ) ) ;
546+
547+ const config = init . processAnswers ( answers ) ;
548+ const filePath = path . resolve ( fixtureDir , ".eslintrc.cjs" ) ;
549+
550+ init . writeFile ( config , answers . format ) ;
551+
552+ assert . isTrue ( fs . existsSync ( filePath ) ) ;
553+
554+ fs . unlinkSync ( filePath ) ;
555+ fs . unlinkSync ( pkgJSONPath ) ;
556+ } ) ;
557+
558+ it ( "should create .eslintrc.json even with type: 'module'" , ( ) => {
559+ answers . format = "JSON" ;
560+
561+ // create package.json with "type": "module"
562+ pkgJSONContents . type = "module" ;
563+
564+ fs . writeFileSync ( pkgJSONPath , JSON . stringify ( pkgJSONContents ) ) ;
565+
566+ const config = init . processAnswers ( answers ) ;
567+ const filePath = path . resolve ( fixtureDir , ".eslintrc.json" ) ;
568+
569+ init . writeFile ( config , answers . format ) ;
570+
571+ assert . isTrue ( fs . existsSync ( filePath ) ) ;
572+
573+ fs . unlinkSync ( filePath ) ;
574+ fs . unlinkSync ( pkgJSONPath ) ;
575+ } ) ;
576+ } ) ;
458577} ) ;
0 commit comments