-
Notifications
You must be signed in to change notification settings - Fork 0
Conversation
.get('/search/shows?q=Friends') | ||
.reply(200, { | ||
results: 'any thing so that I can see this fail at least', | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@n8rzz I need help. I can't get nock
to work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe none of the axios request are being made within jest? If I remove nock and check if the request was actually made I don't get anything even if I add a try/catch within requestAction
. No code gets called after the await. https://github.com/codeBelt/nextjs-mobx/blob/master/src/utilities/mobxUtil.ts#L19
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how does async
work with mobx here? .serach()
isn't listed as async, however it does end up calling ._requestData()
which is. perhaps that's causing issues.
everything else checks out. your tests are passing (for me at least) and everything seems to be working ok, minus the async item above. One other thing to call out, nock
only nocks one time. So you may want to abstract anything nock related into a function that runs the nock stuff and can be easily repeated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think nock
is working. I update this test with how I would like it to fully work. Notice the before and after comments. nock
is not returning the expectedData
. What I am getting is an HttpErrorResponseModel and don't know why when nock
should be catching the request
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixed my issue: axios/axios#1754 (comment)
Need to change the jest environment:
https://jestjs.io/docs/en/configuration.html#testenvironment-string
/**
* @jest-environment node
*/
@@ -0,0 +1,31 @@ | |||
export const SearchLocalStoreMockData = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@n8rzz where should I put mock data files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For mocks shared by more than one domain, I prefer a root dir called __mock__
. I'll also put a __fixtures__
dir at the root as well so that each is easily accessible by other tests that may need them. For domain-specific mocks, I like another __mocks__
dir near the domain (like stores
, etc).
There very likely are cases where root a __mocks__
dir doesn't make sense, but frequently you will have an entity used in multiple domains across an application. Think of a Project
model that would be needed for tests within the stores
, but also tests with components (src/stores
and src/components
).
https://semaphoreci.com/community/tutorials/how-to-test-react-and-mobx-with-jest
https://codeburst.io/testing-mocking-http-requests-with-nock-480e3f164851
https://github.com/nock/nock
https://dev.to/briwa/my-whoa-i-didn-t-know-that-moments-with-jest-3a3h
https://benmccormick.org/2017/08/15/jest-matchers-1/
https://medium.com/@boriscoder/the-hidden-power-of-jest-matchers-f3d86d8101b0