diff --git a/spec/main.spec.ts b/spec/main.spec.ts index 9ff1cce..3408746 100644 --- a/spec/main.spec.ts +++ b/spec/main.spec.ts @@ -134,14 +134,33 @@ describe('main', () => { }); describe('#mockConfig', () => { - after(() => { + let config: Record; + + beforeEach(() => { + config = { foo: { bar: 'faz ' } }; + }); + + afterEach(() => { delete process.env.CLOUD_RUNTIME_CONFIG; }); it('should mock functions.config()', () => { - const config = { foo: { bar: 'faz ' } }; mockConfig(config); expect(functions.config()).to.deep.equal(config); }); + + it('should purge singleton config object when it is present', () => { + mockConfig(config); + config.foo = { baz: 'qux' }; + mockConfig(config); + + expect(functions.config()).to.deep.equal(config); + }); + + it('should not throw an error when functions.config.singleton is missing', () => { + delete functions.config.singleton; + + expect(() => mockConfig(config)).to.not.throw(Error); + }); }); }); diff --git a/src/main.ts b/src/main.ts index 34100e4..64129f7 100644 --- a/src/main.ts +++ b/src/main.ts @@ -22,7 +22,7 @@ import { has, merge, random, get } from 'lodash'; -import { CloudFunction, EventContext, Change, https } from 'firebase-functions'; +import { CloudFunction, EventContext, Change, https, config } from 'firebase-functions'; /** Fields of the event context that can be overridden/customized. */ export type EventContextOptions = { @@ -237,6 +237,10 @@ export function makeChange(before: T, after: T): Change { } /** Mock values returned by `functions.config()`. */ -export function mockConfig(config: { [key: string]: { [key: string]: any } }) { - process.env.CLOUD_RUNTIME_CONFIG = JSON.stringify(config); +export function mockConfig(conf: { [key: string]: { [key: string]: any } }) { + if (config.singleton) { + delete config.singleton; + } + + process.env.CLOUD_RUNTIME_CONFIG = JSON.stringify(conf); }