Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not work with "parameterized config" #196

Open
sbuys opened this issue Mar 2, 2023 · 3 comments
Open

Does not work with "parameterized config" #196

sbuys opened this issue Mar 2, 2023 · 3 comments

Comments

@sbuys
Copy link

sbuys commented Mar 2, 2023

Version info

firebase-functions-test:
3.0.0

firebase-functions:
4.2.1

firebase-admin:
11.5.0

Steps to reproduce

  1. Create a function that uses parameterized config. The current recommended approach for defining configuration.
import * as functions from 'firebase-functions'
import { defineString } from 'firebase-functions/params'

const FOO = defineString('FOO')

export const exampleFn = functions.https.onCall(() => {
  const message = FOO.value()

  functions.logger.log(message)
  
  return {
    message
  }
}
  1. Create .env file with variable defined OR set via cli when deploying with firebase-tools
FOO="bar"

The above works when deployed.

  1. Now when attempting to write a unit test for the above function, there is no mechanism for loading configuration parameters. The older (no longer recommended method) for defining configuration via functions.config can be mocked as described here.

Expected behavior

firebase-functions-test provides a mechanism for loading "parameterized config" into a test environment similar to mockConfig()

Actual behavior

firebase-functions-test DOES NOT provide any mechanism for loading "parameterized config" into a test environment similar to mockConfig()

@wneild
Copy link

wneild commented Jun 6, 2023

You can simply use a library like dotenv to load your environment config now. As long as the properties are set in process.env, your parameterized config will pick it up.

@sceee
Copy link
Contributor

sceee commented Jan 19, 2024

@wneild 's solution seems to actually be a pretty good workaround until firebase-functions-test support a similar function as mockConfig() for parametrized config.

If someone else is looking for this, I ended up with this:

import * as fft from 'firebase-functions-test'

const projectConfig = {
  projectId: 'my-project-id',
  storageBucket: 'my-project-id.appspot.com',
}
export const testFunctions = fft(projectConfig)
// First load .env.local
dotenv.config({
  path: path.resolve(process.cwd(), '.env.local'),
})
// Then load .env if there are other variables defined. This does not overwrite variables already defined in .env.local
dotenv.config({
  path: path.resolve(process.cwd(), '.env'),
})

This makes the parametrized config like defineBoolean('MY_CUSTOM_ENV_VAR') work in tested functions.

@csimmons0
Copy link

How can we make this work with onInit? My module's onInit function, which I'm using as documented here to make parameterized config work, doesn't get called when I run tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants