Closed dhythm closed 2 years ago
Hey @dhythm. I finally had some time to take a look. I managed to get it working with the following changes:
import getCurrentUser from "app/users/queries/getCurrentUser"
import { screen } from "test/utils"
jest.mock("app/users/queries/getCurrentUser")
const mockUseGetUser = getCurrentUser as jest.MockedFunction<typeof getCurrentUser>
test("renders blitz documentation link", async () => {
mockUseGetUser.mockResolvedValue({
id: 1,
name: "User",
email: "user@email.com",
role: "user",
})
const logoutButton = await screen.findByRole("button", { name: "Logout" })
expect(logoutButton).toBeInTheDocument()
})
mockRehectedValue
to mockResolvedValue
.Logout
.Let me know if that helps.
Hi @beerose. Thanks for having time about this issue. Sorry, using mockRejectedValue
is my failure 🙇♂️
I tried the test again with the following code. However, it was failed yet.
import getCurrentUser from "app/users/queries/getCurrentUser"
import { render, screen } from "test/utils"
import Home from "."
jest.mock("app/users/queries/getCurrentUser")
const mockGetCurrentUser = getCurrentUser as jest.MockedFunction<typeof getCurrentUser>
test("renders blitz documentation link", async () => {
// This is an example of how to ensure a specific item is in the document
// But it's disabled by default (by test.skip) so the test doesn't fail
// when you remove the the default content from the page
render(<Home />)
// This is an example on how to mock api hooks when testing
mockGetCurrentUser.mockResolvedValue({
id: 1,
name: "User",
email: "user@email.com",
role: "user",
})
const logoutButton = await screen.findByRole("button", { name: "Logout" })
expect(logoutButton).toBeInTheDocument()
})
At blitz v0.44.4
And also tried at blitz v0.45.2 (created by blitz new
again)
It looks not to mock getCurrentUser
🤦♂️
Hi @dhythm, I created a PR to your repo with all my code changes: https://github.com/dhythm/blitz-sample/pull/1. You'd need to run blitz codegen
(to generate routes and fix the error on your last screenshot) before running the test
Hi @beerose, I've confirmed your PR and the test worked well!!
I guess blitz codegen
seems important for this case. So, I'll try to mock RPC resolver for other repo (much more complicated one).
Thanks for your help!!
No problem!
@beerose and folks! I just found another issue 🤦♂️ , so re-open this issue.
When UserInfo
has two useQuery
s like
const currentUser = useCurrentUser()
const users = useUsers()
and index.test.tsx
mocks them as
jest.mock("app/users/queries/getCurrentUser")
const mockGetCurrentUser = getCurrentUser as jest.MockedFunction<typeof getCurrentUser>
jest.mock("app/users/queries/getUsers")
const mockGetUsers = getUsers as jest.MockedFunction<typeof getUsers>
...
mockGetCurrentUser.mockResolvedValue({
id: 1,
name: "User",
email: "user@email.com",
role: "user",
})
mockGetUsers.mockResolvedValue([
{
id: 1,
name: "User",
email: "user@email.com",
role: "user",
},
{
id: 2,
name: "User2",
email: "user2@email.com",
role: "user2",
},
])
Run the test and watch console.log, then the log returns
{
currentUser: { id: 1, name: 'User', email: 'user@email.com', role: 'user' },
users: { id: 1, name: 'User', email: 'user@email.com', role: 'user' }
}
Looks like useQuery
always returns the first mock 👀
I meet this issue in another repo. (It would happen not only this repo but also other repos.)
If I mock useCurrentUser hook instead of getCurrentUser, then getUser is correctly mocked.
The latest code is here: https://github.com/dhythm/blitz-sample/blob/master/app/pages/index.test.tsx
If it is good to raise a new issue, I'll be happy to do 👍
open a new issue blitz-js/blitz#3148, so close this issue
What is the problem?
As I create a discussion https://github.com/blitz-js/blitz/discussions/3112#discussioncomment-1924103, the mock for query/mutation resolver does not work at blitz 0.44.4. (it works well at older blitz version.) It might be a regression. So I create an issue 🙏
Paste all your error logs here:
Paste all relevant code snippets here:
https://github.com/dhythm/blitz-sample
What are detailed steps to reproduce this?
index.test.tsx
getCurrentUser
instead ofuseCurrentUser
Run
blitz -v
and paste the output here:Please include below any other applicable logs and screenshots that show your problem:
No response