Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jest.fn() is not a function #4613

Closed
burakuluu opened this issue Oct 6, 2017 · 9 comments
Closed

jest.fn() is not a function #4613

burakuluu opened this issue Oct 6, 2017 · 9 comments

Comments

@burakuluu
Copy link

I'm trying to simulate 'onSubmit' by using jest but it throws an error on 'jest.fn()' saying jest.fn() is not a function but in every example, I see everyone imports jest and uses it. Is this a bug or do I miss something?

"jest": "^21.2.1"

await postLogin()
            .then((response) => {
                expect(response.status).toEqual(200)
            })
            .catch((err) => {
                const onClick = jest.fn();
                const testComp = mount(<LoginPage onSubmit={ onClick } />);
                wrapper.find('Form').simulate('submit');
                expect(onClick).toHaveBeenCalled();
                expect(testComp.state('errorValidation')).toBe(true)
                //expect(wrapper.contains(<Alert message='Error' description="Login is failed." type="error" showIcon />)).toBe(true)
            });
@cpojer
Copy link
Member

cpojer commented Oct 6, 2017

Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions. Thank you :)

@cpojer cpojer closed this as completed Oct 6, 2017
@1602
Copy link

1602 commented Feb 25, 2018

@cpojer this is a real issue with documentation, since there's no fn method on jest, but documentation claims that user should be able to run, jest.fn().

It is fair to assume that jest = require('jest'), which is what most of newcomer try to do, and stackoverflow is not being helpful so far: https://stackoverflow.com/questions/46086970/getting-typeerror-jest-fn-is-not-a-function/48976801#48976801

I think documentation needs to be updated.

@rickhanlonii
Copy link
Member

@1602 the docs here say:

The jest object is automatically in scope within every test file.

I'm not aware of any docs that say to import jest, and new users following the getting started guide will not be instructed to

If you think this can be more clear, happy to review a PR with a suggestion!

@1602
Copy link

1602 commented Feb 25, 2018

Thanks @rickhanlonii now I see there confusion comes from! Some users like me are coming from expect.js, while maintaining some legacy code written with mocha and expect. I still use jest-mock which provides jest.fn() capability to be able to spy on functions in a way described in the docs. I guess it is not a primary use case for this testing platform.

@jose920405
Copy link

in my case is because in the setupFiles i was using jest.fn().mockImplementation. Remove that and works

@a1300
Copy link

a1300 commented Aug 27, 2018

I had the same problem. I migrated from mocha to jest and had in the beforeEach function a description string:

Throws error:

beforeEach('setup delegate.js', () => {
});

// correct for jest
beforeEach(() => {
});

The error:

TypeError: fn.call is not a function
at resolve (node_modules/jest-jasmine2/build/queue_runner.js:56:12)

@SimenB
Copy link
Member

SimenB commented Aug 28, 2018

@a1300 your issue is separate (but valid!). Wanna send a PR giving a better error?

For Jest Circus the code is here: https://github.com/facebook/jest/blob/c93e02718480b9f25b0d28f33456ed159bd11b69/packages/jest-circus/src/index.js#L42-L48
For Jest Jasmine it's here: https://github.com/facebook/jest/blob/c93e02718480b9f25b0d28f33456ed159bd11b69/packages/jest-jasmine2/src/jasmine/Env.js#L505-L539


If you don't want to send a PR, can you open up a separate issue?

@a1300
Copy link

a1300 commented Aug 28, 2018

@SimenB thanks for the information.

I will open a separate issue.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants