22 * Copyright (c) Neil Enns. All rights reserved.
33 * Licensed under the MIT License. See LICENSE in the project root for license information.
44 *--------------------------------------------------------------------------------------------*/
5- import { closeSync , existsSync , openSync , unlinkSync , writeFileSync } from 'fs' ;
5+ import { closeSync , existsSync , openSync , unlinkSync , writeFileSync } from "fs" ;
66import * as JSONC from "jsonc-parser" ;
77import * as helpers from "./../src/helpers" ;
88
99describe ( "helpers" , ( ) => {
1010 const settingsFilePath = `${ __dirname } /settings.json` ;
1111
1212 beforeEach ( ( ) => {
13- closeSync ( openSync ( settingsFilePath , 'w' ) ) ;
13+ closeSync ( openSync ( settingsFilePath , "w" ) ) ;
1414 } ) ;
1515
1616 afterEach ( ( ) => {
@@ -20,71 +20,77 @@ describe("helpers", () => {
2020 }
2121 } ) ;
2222
23- test ( "Verify can load settings.json" , ( ) => {
24- const serviceName = "Settings" ;
25- const expectedSettings = { " foo" : "bar" } ;
26- writeFileSync ( settingsFilePath , JSON . stringify ( expectedSettings ) ) ;
23+ test ( "Verify can load settings.json" , ( ) => {
24+ const serviceName = "Settings" ;
25+ const expectedSettings = { foo : "bar" } ;
26+ writeFileSync ( settingsFilePath , JSON . stringify ( expectedSettings ) ) ;
2727
28- const actualSettings = helpers . readSettings ( serviceName , settingsFilePath ) ;
28+ const actualSettings = helpers . readSettings ( serviceName , settingsFilePath ) ;
2929
30- expect ( actualSettings ) . toEqual ( expectedSettings ) ;
31- } ) ;
30+ expect ( actualSettings ) . toEqual ( expectedSettings ) ;
31+ } ) ;
3232
33- test ( "Verify cannot load settings.json because it does not exist" , ( ) => {
34- //eslint-disable-next-line no-console
35- console . log = jest . fn ( ) ;
36- const serviceName = "Settings" ;
37- unlinkSync ( settingsFilePath ) ;
33+ test ( "Verify cannot load settings.json because it does not exist" , ( ) => {
34+ //eslint-disable-next-line no-console
35+ console . log = jest . fn ( ) ;
36+ const serviceName = "Settings" ;
37+ unlinkSync ( settingsFilePath ) ;
3838
39- const actualSettings = helpers . readSettings ( serviceName , settingsFilePath ) ;
39+ const actualSettings = helpers . readSettings ( serviceName , settingsFilePath ) ;
4040
41- //eslint-disable-next-line no-console
42- expect ( console . log ) . toHaveBeenCalledWith ( expect . stringContaining ( "[Settings] Unable to read the configuration file: ENOENT: no such file or directory" ) ) ;
43- expect ( actualSettings ) . toBeNull ( ) ;
44- } ) ;
41+ //eslint-disable-next-line no-console
42+ expect ( console . log ) . toHaveBeenCalledWith (
43+ expect . stringContaining ( "[Settings] Unable to read the configuration file: ENOENT: no such file or directory" ) ,
44+ ) ;
45+ expect ( actualSettings ) . toBeNull ( ) ;
46+ } ) ;
4547
46- test ( "Verify throws if rawConfig empty" , ( ) => {
47- const serviceName = "Settings" ;
48- const expectedSettings = "" ;
49- writeFileSync ( settingsFilePath , expectedSettings ) ;
48+ test ( "Verify throws if rawConfig empty" , ( ) => {
49+ const serviceName = "Settings" ;
50+ const expectedSettings = "" ;
51+ writeFileSync ( settingsFilePath , expectedSettings ) ;
5052
51- expect ( ( ) => { helpers . readSettings ( serviceName , settingsFilePath ) } ) . toThrow ( Error ) ;
52- } ) ;
53+ expect ( ( ) => {
54+ helpers . readSettings ( serviceName , settingsFilePath ) ;
55+ } ) . toThrow ( Error ) ;
56+ } ) ;
5357
54- test ( "Verify throws with message if rawConfig empty" , ( ) => {
55- const serviceName = "Settings" ;
56- const expectedSettings = "" ;
57- writeFileSync ( settingsFilePath , expectedSettings ) ;
58+ test ( "Verify throws with message if rawConfig empty" , ( ) => {
59+ const serviceName = "Settings" ;
60+ const expectedSettings = "" ;
61+ writeFileSync ( settingsFilePath , expectedSettings ) ;
5862
59- try {
60- helpers . readSettings ( serviceName , settingsFilePath ) ;
61- } catch ( error ) {
62- expect ( error . message ) . toBe ( `[${ serviceName } ] Unable to load configuration file ${ settingsFilePath } .` ) ;
63- }
64- } ) ;
63+ try {
64+ helpers . readSettings ( serviceName , settingsFilePath ) ;
65+ } catch ( error ) {
66+ expect ( error . message ) . toBe ( `[${ serviceName } ] Unable to load configuration file ${ settingsFilePath } .` ) ;
67+ }
68+ } ) ;
6569
6670 test ( "Verify throws if json invalid" , ( ) => {
6771 const serviceName = "Settings" ;
6872 const expectedSettings = { } ;
6973 writeFileSync ( settingsFilePath , JSON . stringify ( expectedSettings ) ) ;
70- const mockAddListener = jest . spyOn ( JSONC , ' parse' ) ;
74+ const mockAddListener = jest . spyOn ( JSONC , " parse" ) ;
7175 mockAddListener . mockImplementation ( ( rawConfig , parseErrors ) => {
72- parseErrors . push ( {
73- error : 1 ,
74- offset : 2 ,
75- length : 3 ,
76- } ) ;
77- return { } ;
76+ parseErrors . push ( {
77+ error : 1 ,
78+ offset : 2 ,
79+ length : 3 ,
80+ } ) ;
81+ return { } ;
7882 } ) ;
7983
80- expect ( ( ) => { helpers . readSettings ( serviceName , settingsFilePath ) } ) . toThrow ( Error ) ;
84+ expect ( ( ) => {
85+ helpers . readSettings ( serviceName , settingsFilePath ) ;
86+ } ) . toThrow ( Error ) ;
8187 } ) ;
8288
8389 test ( "Verify throws with message if json invalid" , ( ) => {
8490 const serviceName = "Settings" ;
8591 const expectedSettings = { } ;
8692 writeFileSync ( settingsFilePath , JSON . stringify ( expectedSettings ) ) ;
87- const mockAddListener = jest . spyOn ( JSONC , ' parse' ) ;
93+ const mockAddListener = jest . spyOn ( JSONC , " parse" ) ;
8894 const parseError1 = {
8995 error : 1 ,
9096 offset : 2 ,
@@ -96,21 +102,20 @@ describe("helpers", () => {
96102 length : 1 ,
97103 } ;
98104 mockAddListener . mockImplementation ( ( rawConfig , parseErrors ) => {
99- parseErrors . push ( parseError1 ) ;
100- parseErrors . push ( parseError2 ) ;
101- return { } ;
105+ parseErrors . push ( parseError1 ) ;
106+ parseErrors . push ( parseError2 ) ;
107+ return { } ;
102108 } ) ;
103109 //eslint-disable-next-line no-console
104110 console . log = jest . fn ( ) ;
105111
106- try {
107- helpers . readSettings ( serviceName , settingsFilePath ) ;
108- } catch ( error ) {
109- const parseErrorsAsString = `${ JSON . stringify ( parseError1 ) } ${ JSON . stringify ( parseError2 ) } ` ;
110- //eslint-disable-next-line no-console
111- expect ( console . log ) . toHaveBeenCalledWith ( expect . stringContaining ( `[${ serviceName } ] ${ parseErrorsAsString } ` ) ) ;
112- expect ( error . message ) . toBe ( `[${ serviceName } ] Unable to load configuration file: ${ parseErrorsAsString } ` ) ;
113- }
112+ try {
113+ helpers . readSettings ( serviceName , settingsFilePath ) ;
114+ } catch ( error ) {
115+ const parseErrorsAsString = `${ JSON . stringify ( parseError1 ) } ${ JSON . stringify ( parseError2 ) } ` ;
116+ //eslint-disable-next-line no-console
117+ expect ( console . log ) . toHaveBeenCalledWith ( expect . stringContaining ( `[${ serviceName } ] ${ parseErrorsAsString } ` ) ) ;
118+ expect ( error . message ) . toBe ( `[${ serviceName } ] Unable to load configuration file: ${ parseErrorsAsString } ` ) ;
119+ }
114120 } ) ;
115121} ) ;
116-
0 commit comments