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

initializeTestApp from @firebase/rules-unit-testing with jest, file scope variables are unset #3920

ScottPierce opened this issue Oct 10, 2020 · 4 comments · Fixed by #3923
api: firestore testing-sdk testing with emulator


Copy link

ScottPierce commented Oct 10, 2020

[REQUIRED] Describe your environment

  • Operating System version: OSX
  • Browser version: This is for test
  • Firebase SDK version:
        "firebase": "^7.23.0",
        "firebase-admin": "^9.2.0",
        "@firebase/rules-unit-testing": "^1.0.7",
        "@firebase/testing": "^0.20.11",
  • Firebase Product: firestore
  • Running tests with Jest inside of a NX workspace library

[REQUIRED] Describe the problem

When I use initializeTestApp with the @firebase/rules-unit-testing, any variables that I set in the root scope of the file are invalidated in jest tests. The first test will pass, but then, when the second test is run, and initializeTestApp is called, the second test won't pass because file scoped variables are unset.

  • In the second test, if initializeTestApp is removed, the test passes just fine.
  • If the import is changed from @firebase/rules-unit-testing to @firebase/testing, the test passes just fine.

Likely Unrelated:
I'm doing this in an nx react application.

Steps to reproduce:

Run the given code sample with Jest. The second test will fail, even though it shouldn't, because testData.uid will be unset.

Again, simply changing the import from rules-unit-testing to testing fixes the problem.

Relevant Code:

import {
} from "@firebase/rules-unit-testing"

const testData = { uid: "UserId" }
const projectId = `user-feedback-db`
const myAuth = { uid: "user_me", email: "" }

test("test1", () => {
        projectId: projectId,
        auth: myAuth

test("test2", () => {
        projectId: projectId,
        auth: myAuth
Copy link

n0mer commented Oct 12, 2020

@ScottPierce i have the similar problem. Try downgrading @firebase/rules-unit-testing from 1.0.7 to 1.0.6, until this is fixed.

This serves as changelog:

@thebrianchen thebrianchen added the testing-sdk testing with emulator label Oct 12, 2020
Copy link

samtstern commented Oct 12, 2020

@ScottPierce sorry about that this one is my fault, fixing it here:

In the meantime if you downgrade your rules-unit-testing dependency to 1.0.5 it should not have that bug.

Copy link

ScottPierce commented Oct 12, 2020

How is this even possible? How did you unset variables in the parent scope, a const no less. I'm new to js / ts.

Copy link

samtstern commented Oct 12, 2020

If you look at the PR you'll see I was doing delete auth.uid which removes the uid key from the object. I now make a copy before doing that.

const doesn't protect your obejct from being mutated, it only protects the reference from being reset. So:

const x = { a: 1 };

// This is allowed
x.a = 2;

// This is not allowed
x = { a: 2 };

@firebase firebase locked and limited conversation to collaborators Nov 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
api: firestore testing-sdk testing with emulator
None yet

Successfully merging a pull request may close this issue.

5 participants