5

I have a custom Hook in a file and I have to mock it quite often for multiple tests. Can I have the mock in a separate file and then just import it into the test?

Hook file

export default function myHook(key) {
    const { state, app } = StateFromSomePlace();

    const setParameter = useCallback(
        newValue => {
            // do something
        },
        [app, key, state.something]
    );

    if (key) {
       // do something
        const parameter = // something;
        return [parameter, setParameter];
    }
}

i want to mock the above using jest to return something (which i can), but i want to do so in an helper/setup file so i can use it in multiple test by just importing the mock.

Thanks for the help in advance. :)

Kush Singh
  • 157
  • 3
  • 11

1 Answers1

-3

https://jestjs.io/docs/manual-mocks

You can as per the documentation Follow the instruction

viren
  • 113
  • 4
  • I don't think the documentation that is linked helps here. I have the same issue and wanted instead of mocking each time the authentication and the db layer that sometimes need to be mocked to have them in another file and just import the file. @kush-singh did you ever find a solution? – M.C. Dec 11 '21 at 12:47