Skip to content

Commit 2574781

Browse files
authored
Remove functions.config.singleton for getting recent value of CLOUD_RUNTIME_CONFIG (#74)
1 parent 012d3ae commit 2574781

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

spec/main.spec.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,33 @@ describe('main', () => {
134134
});
135135

136136
describe('#mockConfig', () => {
137-
after(() => {
137+
let config: Record<string, unknown>;
138+
139+
beforeEach(() => {
140+
config = { foo: { bar: 'faz ' } };
141+
});
142+
143+
afterEach(() => {
138144
delete process.env.CLOUD_RUNTIME_CONFIG;
139145
});
140146

141147
it('should mock functions.config()', () => {
142-
const config = { foo: { bar: 'faz ' } };
143148
mockConfig(config);
144149
expect(functions.config()).to.deep.equal(config);
145150
});
151+
152+
it('should purge singleton config object when it is present', () => {
153+
mockConfig(config);
154+
config.foo = { baz: 'qux' };
155+
mockConfig(config);
156+
157+
expect(functions.config()).to.deep.equal(config);
158+
});
159+
160+
it('should not throw an error when functions.config.singleton is missing', () => {
161+
delete functions.config.singleton;
162+
163+
expect(() => mockConfig(config)).to.not.throw(Error);
164+
});
146165
});
147166
});

src/main.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import { has, merge, random, get } from 'lodash';
2424

25-
import { CloudFunction, EventContext, Change, https } from 'firebase-functions';
25+
import { CloudFunction, EventContext, Change, https, config } from 'firebase-functions';
2626

2727
/** Fields of the event context that can be overridden/customized. */
2828
export type EventContextOptions = {
@@ -237,6 +237,10 @@ export function makeChange<T>(before: T, after: T): Change<T> {
237237
}
238238

239239
/** Mock values returned by `functions.config()`. */
240-
export function mockConfig(config: { [key: string]: { [key: string]: any } }) {
241-
process.env.CLOUD_RUNTIME_CONFIG = JSON.stringify(config);
240+
export function mockConfig(conf: { [key: string]: { [key: string]: any } }) {
241+
if (config.singleton) {
242+
delete config.singleton;
243+
}
244+
245+
process.env.CLOUD_RUNTIME_CONFIG = JSON.stringify(conf);
242246
}

0 commit comments

Comments
 (0)