Mock auth() instance Jest #5851
Replies: 3 comments 3 replies
-
Hi there! We use jest as part of our testing strategy internally, and our jest.setup.ts may serve as an example https://github.com/invertase/react-native-firebase/blob/main/jest.setup.ts Hopefully that gets you going |
Beta Was this translation helpful? Give feedback.
-
Thank you, Mike, for reply. There is a little confusion, it seemed necessary to simply mock the result of the function, but something is not easy for me to implement... I don't need to mock the function if I set up the jest setup this way? Maybe you have an example of a test written with the signInWithCredential function? |
Beta Was this translation helpful? Give feedback.
-
@bad-encoder I realize this is quite late but for anyone else coming through this way, maybe this will help. Personally I found the jest.setup referenced to be quite confusing, it's not how I'm used to doing mocks so I didn't really know how to make use of it. That said, testing and mocking is not my forte! I did something like this (we're using ts-jest): // Component.test.tsx
import { mocked } from 'ts-jest/utils';
import auth from '../../../utils/firebaseAuth'; // In my case I'm importing from here, importing the library directly should also work
jest.mock('../../../utils/firebaseAuth');
const mockedAuth = mocked(auth, true);
beforeEach(() => {
mockedAuth.mockReset();
})
it('should do something', () => {
mockedAuth.mockReturnValue({
currentUser: null, // here you can put what you need
onAuthStateChanged: jest.fn(),
} as unknown as FirebaseAuthTypes.Module);
// ... rest of your test
}) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, when I try to mock provider methods - everything works , but it doesn't work with methods of auth() instance
How do I do this so I don't get an error:
TypeError: (0 , _auth.default) is not a function
test:
code:
Beta Was this translation helpful? Give feedback.
All reactions