2222
2323import { expect } from 'chai' ;
2424import * as fs from 'fs' ;
25- import * as mockRequire from 'mock-require ' ;
25+ import * as process from 'process ' ;
2626import Sinon = require( 'sinon' ) ;
2727
2828import * as config from '../../src/v1/config' ;
2929
3030describe ( 'config()' , ( ) => {
3131 let readFileSync : Sinon . SinonStub ;
32+ let cwdStub : Sinon . SinonStub ;
3233
3334 before ( ( ) => {
3435 readFileSync = Sinon . stub ( fs , 'readFileSync' ) ;
3536 readFileSync . throws ( 'Unexpected call' ) ;
36- process . env . PWD = '/srv' ;
37+ cwdStub = Sinon . stub ( process , 'cwd' ) ;
38+ cwdStub . returns ( '/srv' ) ;
3739 } ) ;
3840
3941 after ( ( ) => {
40- delete process . env . PWD ;
4142 Sinon . verifyAndRestore ( ) ;
4243 } ) ;
4344
4445 afterEach ( ( ) => {
45- mockRequire . stopAll ( ) ;
4646 delete config . config . singleton ;
4747 ( config as any ) . firebaseConfigCache = null ;
4848 delete process . env . FIREBASE_CONFIG ;
4949 delete process . env . CLOUD_RUNTIME_CONFIG ;
5050 } ) ;
5151
5252 it ( 'loads config values from .runtimeconfig.json' , ( ) => {
53- mockRequire ( '/srv/.runtimeconfig.json' , { foo : 'bar' , firebase : { } } ) ;
53+ const json = JSON . stringify ( {
54+ foo : 'bar' ,
55+ firebase : { } ,
56+ } ) ;
57+ readFileSync
58+ . withArgs ( '/srv/.runtimeconfig.json' )
59+ . returns ( Buffer . from ( json ) ) ;
5460 const loaded = config . config ( ) ;
5561 expect ( loaded ) . to . not . have . property ( 'firebase' ) ;
5662 expect ( loaded ) . to . have . property ( 'foo' , 'bar' ) ;
5763 } ) ;
5864
5965 it ( 'does not provide firebase config if .runtimeconfig.json not invalid' , ( ) => {
60- mockRequire ( '/srv/.runtimeconfig.json' , 'does-not-exist ') ;
66+ readFileSync . withArgs ( '/srv/.runtimeconfig.json' ) . returns ( 'invalid JSON ') ;
6167 expect ( config . firebaseConfig ( ) ) . to . be . null ;
6268 } ) ;
6369
6470 it ( 'does not provide firebase config if .ruuntimeconfig.json has no firebase property' , ( ) => {
65- mockRequire ( '/srv/.runtimeconfig.json' , { } ) ;
71+ readFileSync
72+ . withArgs ( '/srv/.runtimeconfig.json' )
73+ . returns ( Buffer . from ( '{}' ) ) ;
6674 expect ( config . firebaseConfig ( ) ) . to . be . null ;
6775 } ) ;
6876
@@ -78,7 +86,7 @@ describe('config()', () => {
7886
7987 it ( 'loads Firebase configs from FIREBASE_CONFIG env variable pointing to a file' , ( ) => {
8088 const oldEnv = process . env ;
81- process . env = {
89+ ( process as any ) . env = {
8290 ...oldEnv ,
8391 FIREBASE_CONFIG : '.firebaseconfig.json' ,
8492 } ;
@@ -91,13 +99,14 @@ describe('config()', () => {
9199 'foo@firebaseio.com'
92100 ) ;
93101 } finally {
94- process . env = oldEnv ;
102+ ( process as any ) . env = oldEnv ;
95103 }
96104 } ) ;
97105
98106 it ( 'accepts alternative locations for config file' , ( ) => {
99107 process . env . CLOUD_RUNTIME_CONFIG = 'another.json' ;
100- mockRequire ( 'another.json' , { foo : 'bar' , firebase : { } } ) ;
108+ const json = JSON . stringify ( { foo : 'bar' , firebase : { } } ) ;
109+ readFileSync . withArgs ( 'another.json' ) . returns ( Buffer . from ( json ) ) ;
101110 expect ( config . firebaseConfig ( ) ) . to . not . be . null ;
102111 expect ( config . config ( ) ) . to . have . property ( 'foo' , 'bar' ) ;
103112 } ) ;
0 commit comments