55 promises : { readFile }
66} = require ( "fs" ) ;
77const path = require ( "path" ) ;
8+ const sinon = require ( "sinon" ) ;
9+ let sandbox = sinon . createSandbox ( ) ;
810
911describe ( "server" , ( ) => {
1012 // 3.1
@@ -27,8 +29,23 @@ describe("server", () => {
2729 assert . ok ( response . text . match ( / b o d y { / ) ) ;
2830 } ) ;
2931 } ) ;
30- // 5.1
32+ // 6.5
33+ it ( "should load the dotenv config" , async ( ) => {
34+ const serverFilePath = path . join ( process . cwd ( ) , "src" , "server.js" ) ;
35+ const serverFile = await readFile ( serverFilePath , "utf8" ) ;
36+ const lines = serverFile . split ( "/n" ) ;
37+ const firstLines = lines . slice ( 0 , 5 ) ;
38+ let match = false ;
39+ for ( const line of firstLines ) {
40+ if ( line . match ( / r e q u i r e .+ d o t e n v .+ \. c o n f i g ( ) / ) ) {
41+ match = true ;
42+ }
43+ }
44+ assert . ok ( match , "should load dotenv config at the top of the file" ) ;
45+ } ) ;
46+ // 6.6
3147 it ( 'should return json from the "/json" endpoint' , ( ) => {
48+ sandbox . stub ( process . env , "MESSAGE_STYLE" ) . value ( "" ) ;
3249 return request ( server )
3350 . get ( "/json" )
3451 . expect ( "Content-type" , / a p p l i c a t i o n \/ j s o n / )
@@ -41,20 +58,23 @@ describe("server", () => {
4158 'Message should be "Hello json"'
4259 ) ;
4360 } ) ;
61+ sandbox . restore ( ) ;
4462 } ) ;
45- // 6.5
46- it ( "should load the dotenv config" , async ( ) => {
47- const serverFilePath = path . join ( process . cwd ( ) , "src" , "server.js" ) ;
48- const serverFile = await readFile ( serverFilePath , "utf8" ) ;
49- const lines = serverFile . split ( "/n" ) ;
50- const firstLines = lines . slice ( 0 , 5 ) ;
51- let match = false ;
52- for ( const line of firstLines ) {
53- if ( line . match ( / r e q u i r e .+ d o t e n v .+ \. c o n f i g ( ) / ) ) {
54- match = true ;
55- }
56- }
57- assert . ok ( match , "should load dotenv config at the top of the file" ) ;
63+ it ( 'should return json from the "/json" endpoint' , ( ) => {
64+ sandbox . stub ( process . env , "MESSAGE_STYLE" ) . value ( "uppercase" ) ;
65+ return request ( server )
66+ . get ( "/json" )
67+ . expect ( "Content-type" , / a p p l i c a t i o n \/ j s o n / )
68+ . expect ( 200 )
69+ . then ( response => {
70+ assert . ok ( response . body , "Body does not contain json" ) ;
71+ assert . equal (
72+ response . body . message ,
73+ "HELLO JSON" ,
74+ 'Message should be "HELLO JSON" when process.env.MESSAGE_STYLE is "uppercase"'
75+ ) ;
76+ } ) ;
77+ sandbox . restore ( ) ;
5878 } ) ;
5979 after ( ( ) => {
6080 server . close ( ) ;
0 commit comments