firebase / firebase-functions-test

MIT License
232 stars 48 forks source link

Mock Config Updates #82

Closed pete-seak closed 3 years ago

pete-seak commented 3 years ago

Issue copied from here

The mocked environment config for firebase appears to only be mutable on initialization. I would like to be able to modify the config for various mocha tests and test different configurations of the code. Here is an example

const chai = require('chai')
const test = require('firebase-functions-test')()
const functions = require('firebase-functions')

describe('All Tests', () => {
    describe('Test1', () => {
        before(function () {
            const config = {
                site: {
                    url: 'https://site1.com'
                }
            }
            test.mockConfig(config)
        })

        it('Run Test', async () => {
            console.log(functions.config().site.url)
            // ...
        })

        after(() => {
            test.cleanup()
        })
    })

    describe('Test2', () => {
        before(function () {
            const config = {
                site: {
                    url: 'https://site2.com'
                }
            }
            test.mockConfig(config)
        })

        it('Run Test', async () => {
            console.log(functions.config().site.url)
        })

        after(() => {
            test.cleanup()
        })
    })
})

The config in 'Test 2' will match that of 'Test 1'. My hope was that test.cleanup() would reset the mocked state and allow it to be changed in further tests. Instead, you will see this output

  All Tests
    Test1
https://site1.com
      ✓ Run Test
    Test2
https://site1.com
      ✓ Run Test

  2 passing (12ms)

I can also confirm that moving the test and functions declarations

const test = require('firebase-functions-test')()
const functions = require('firebase-functions')

into the individual describe blocks does nothing to control the scope. The configs are placed into global scope and are immutable after initialization.

Version info

firebase-functions-test: "firebase-functions-test": "^0.2.1" firebase-functions: "firebase-functions": "^3.11.0" firebase-admin: "firebase-admin": "^8.13.0",

Test case

Please see above

Steps to reproduce

Call test.mockConfig more than once (see code above)

Expected behavior

value read from config is updated

Actual behavior

value read from config is the value set on the first test.mockConfig call

  All Tests
    Test1
https://site1.com
      ✓ Run Test
    Test2
https://site1.com
      ✓ Run Test

  2 passing (12ms)
sbenmoussati commented 3 years ago

@pete-seak : I had the same issue. Upgrading firebase-functions-test lib to the latest version (^0.2.3) fixed it. Hope it helps !

pete-seak commented 3 years ago

@pete-seak : I had the same issue. Upgrading firebase-functions-test lib to the latest version (^0.2.3) fixed it. Hope it helps !

@sbenmoussati Thanks for the update. I confirm that did fix the issue