Skip to content

Commit

Permalink
Remove functions.config.singleton for getting recent value of CLOUD_R…
Browse files Browse the repository at this point in the history
…UNTIME_CONFIG (#74)
  • Loading branch information
saygun authored Oct 6, 2020
1 parent 012d3ae commit 2574781
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
23 changes: 21 additions & 2 deletions spec/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,33 @@ describe('main', () => {
});

describe('#mockConfig', () => {
after(() => {
let config: Record<string, unknown>;

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);
});
});
});
10 changes: 7 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -237,6 +237,10 @@ export function makeChange<T>(before: T, after: T): Change<T> {
}

/** 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);
}

0 comments on commit 2574781

Please sign in to comment.