-
Notifications
You must be signed in to change notification settings - Fork 55
Mock router
to test component using Jest
#136
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
Comments
We use a mock object where we replace all the functions with Does Jest have like "smart" mocks that use object proxies or anything? |
jest will auto mock something intelligently i think based on module exports. e.g. jest.mock('found/router') |
I don't think we export the object anywhere, though. We do export the prop type. Python has "smart" mocks that are essentially proxy objects with all methods auto-mocked. Does such a thing exist in the Node ecosystem? |
i think jest is the best example of it, but it still a bit welded into jest itself. there might be some better optins tho now that Proxy is well supported |
I was able to mock 'found' module by doing jest.mock('found'), but couldn't the router itself. Following @taion comment the test passed (although it seems to me like a temporary solution) by defining router as:
|
I guess we could expose like a |
I can make a PR if you decide to do so. Just want to know which are the objects you want me to expose. Another test related issue I faced was to get an initial state of found to initialize a mockStore configured by |
Well, if you're trying to test state management, it might be easiest to use a server protocol and just actually instantiate Found. |
No problems with Found instance so. TY again @taion . |
For those trying to test components that depend on
|
Okay, I think the thing to do here is to add a |
Along these lines, I'm trying to |
Was there a fix for this? I have tried @gabrielgian suggestion of mocking the router object with with |
@gabrielgian do you have an example you could share or something working? |
So I ended up mocking the
|
That makes sense to me. |
Guys, I want to test this component that is use in my app inside a what is the best way to test it with as little boilerplate code?
I would rather use mount, than doing something like this in the test
|
There's not a really good way to do this at the moment. I'm going to expose the router context as its own thing, and then it will be possible to inject a test router, but right now the best bet is to follow the paths above – either render a server/memory router. Though if you're actually testing navigation, arguably it'd be best to use |
Okay, plan here is to add a |
dummyRouter sounds like a good solution! having trouble implementing the above solutions so one a bit more simple is something to look forward to! |
As an update, I found it much easier to just make a MockRouter class with just the methods I needed. Then in a test,
Worked fine for my use cases. |
Any update on this? |
For anyone who uses jest, this has been working for us: Re anything that returns a
Re anything context related that probably stems from
|
You can mock your react-router with the code below. Let's say all you needed in your component is
|
I want to test my components with Snapshots and am using Jest to do so. Since my component is using withRoute HOC, both my component and somewher in HOC require
router
and all its functions in their in the contexts.Is there a easy way to create an instance of
router
or mock it?Here is a sample of my code:
The text was updated successfully, but these errors were encountered: