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)
});
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions. Thank you :)
@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.
@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!
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.
in my case is because in the setupFiles
i was using jest.fn().mockImplementation
. Remove that and works
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)
@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?
@SimenB thanks for the information.
I will open a separate issue.
Most helpful comment
@cpojer this is a real issue with documentation, since there's no
fn
method onjest
, 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#48976801I think documentation needs to be updated.