javascript - How to spy on an object function returned from a mock in Jest? - Stack Overflow

In my app I have something similar to this to signOutUser a user.import { getAuth } from "firebas

In my app I have something similar to this to signOutUser a user.

import { getAuth } from "firebase/auth";
export function signOutUser(): Promise<void> {
  return getAuth(firebaseApp).signOut();
}

And I want to make sure that the signOut function of firebase is called.

But I can't figure how to mock and spy on the sign out function that is on the auth object.

One attempt I've done...

I'm mocking these functions from firebase/auth like so...

const mockSignOut = jest.fn();
jest.mock("firebase/auth", () => {
  return {
    __esModule: true,
    getAuth: jest.fn().mockImplementation(() => {
      return {
        signOut: mockSignOut,
      };
    }),
  };
});

I get ReferenceError: Cannot access 'mockSignOut' before initialization .

If I change it to var I get TypeError: signOut is not a function

The documentation I think this is kind of close to what I want? But it doesn't explain how you would mock a mocked value that I understand at least

How can I spy on this signOut function from the object that getAuth() returns and is also mocked?

In my app I have something similar to this to signOutUser a user.

import { getAuth } from "firebase/auth";
export function signOutUser(): Promise<void> {
  return getAuth(firebaseApp).signOut();
}

And I want to make sure that the signOut function of firebase is called.

But I can't figure how to mock and spy on the sign out function that is on the auth object.

One attempt I've done...

I'm mocking these functions from firebase/auth like so...

const mockSignOut = jest.fn();
jest.mock("firebase/auth", () => {
  return {
    __esModule: true,
    getAuth: jest.fn().mockImplementation(() => {
      return {
        signOut: mockSignOut,
      };
    }),
  };
});

I get ReferenceError: Cannot access 'mockSignOut' before initialization .

If I change it to var I get TypeError: signOut is not a function

The documentation I think this is kind of close to what I want? But it doesn't explain how you would mock a mocked value that I understand at least https://jestjs.io/docs/es6-class-mocks#manual-mock

How can I spy on this signOut function from the object that getAuth() returns and is also mocked?

Share Improve this question asked Jan 10, 2022 at 1:13 Carson WoodCarson Wood 1,3344 gold badges15 silver badges24 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Jest will automatically hoist jest.mock calls to the top of the module (before any imports). That means jest.mock will be called before the variable is declared. That's why you got the error.

You don't need to provide the mock factory function for jest.mock in your case. Just let the jest create mock for firebase/auth automatically. That means functions exported(getAuth) from the firebase/auth module will be mocked.

Then, you should provide a mock implementation for the getAuth function.

E.g.

index.ts:

import { getAuth } from 'firebase/auth';
import { initializeApp } from 'firebase/app';

const firebaseApp = initializeApp({});

export function signOutUser(): Promise<void> {
  return getAuth(firebaseApp).signOut();
}

index.test.ts:

import { Auth, getAuth } from 'firebase/auth';
import { signOutUser } from './';

jest.mock('firebase/auth');

describe('70646844', () => {
  test('should pass', async () => {
    const mAuth = ({
      signOut: jest.fn(),
    } as unknown) as Auth;
    (getAuth as jest.MockedFunction<typeof getAuth>).mockReturnValue(mAuth);

    await signOutUser();
    expect(getAuth).toBeCalledTimes(1);
    expect(getAuth().signOut).toBeCalledTimes(1);
    // Better
    // expect(mAuth.signOut).toBeCalledTimes(1);
  });
});

test result:

 PASS  examples/70646844/index.test.ts (10.306 s)
  70646844
    ✓ should pass (2 ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |                   
 index.ts |     100 |      100 |     100 |     100 |                   
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        10.512 s, estimated 12 s

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745096568a4611013.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信