Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

311
Views
how to mock firebase .auth().currentUser in .test.js

I want to test the following function

export async function getIdToken() {
  const currentUser = firebase.auth().currentUser;
  if (currentUser) {
    return currentUser.getIdToken();
  } else {
    return undefined;
  }
}

export async function getSomethingViaHTTPS() {
  const idToken = await getIdToken();
  const response = await fetch("https://someURL.com", {
    method: "GET",
    headers: { Authorization: "Bearer " + idToken }
  });
  if (response.status === 200) {
    return await response.json();
  }

  throw new Error("failed to fetch data breaches: " + response.status + ": " + (await response.text()));
}

Now I dont know how I can test it since during test the current user would be undefined. Is there a way to mock the current user? This is my test function:

describe("something via https", () => {
  const mockGetSomethingAPI = responseBody => {
    fetchMock.once(req => {
      if (req.url !== "https://someURL.com") {
        return Promise.reject({});
      }
      return Promise.resolve(JSON.stringify(responseBody));
    });
  };

  it("get something", async () => {
    mockGetSomethingAPI({ "something": "some text" });
    expect(await mockGetSomethingAPI()).toEqual({ something: "testestest" });
  });
});
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!