Skip to content

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

Closed
@ScottPierce

Description

@ScottPierce

[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 {
    apps,
    assertFails,
    assertSucceeds,
    clearFirestoreData,
    initializeAdminApp,
    initializeTestApp,
    loadFirestoreRules,
} from "@firebase/rules-unit-testing"

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

test("test1", () => {
    initializeTestApp({
        projectId: projectId,
        auth: myAuth
    })
    expect(testData.uid).toEqual("UserId")
})

test("test2", () => {
    initializeTestApp({
        projectId: projectId,
        auth: myAuth
    })
    expect(testData.uid).toEqual("UserId")
})

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions