87 questions
0
votes
1
answer
281
views
testing-library/react-hooks calling a function that change the state
I tried everything but still for some reason the react state doesn't change, what is I am missing or what is the alternative approach?
Code:
export const useCounter = (): UseCounterResult => {
...
1
vote
0
answers
397
views
react-hooks-testing-library: Context Provider, state change in child function act() error
I am using the React renderHook() function to call a custom hook. Internally in the custom hook, I call a function that executes axios, gets data and on success, dispatches a state change to a custom ...
1
vote
1
answer
353
views
React testing custom hook with window.url
I have a custom hook that downloads a file from an url, it works fine and now I trying to test its behaviour.
const useFileDownload = ({ apiResponse, fileName }) => {
const ref = useRef(null)
...
0
votes
0
answers
1k
views
result.current.data is undefined when I declare renderHook into the test closure but works when it is defined outside of it
I'm trying to test a custom hook but for some reason I can't render the hook inside the test closure "it".
This code works (I must use the rerender methods otherwise the result.current.data ...
1
vote
1
answer
2k
views
Testing custom hooks react typescript
Im learning react testing and i have this hook i want to test but i have no idea how
import { useState, useCallback } from 'react';
import axios from 'axios';
export const useFetch = () => {
...
1
vote
0
answers
2k
views
Vitest + MSW - Runtime request handler is not working
I'm trying to migrate the test framework of my app from Jest to Vitest (for multiple reasons) and I encounter an issue.
With Jest, I was using runtime request handlers at multiple places of my app to ...
2
votes
1
answer
181
views
react page doesnt render when getting the data back from server
react won't work when rendering the page, but when i changed code in the vscode (added a line of console or comment one line out), the page is rendered. or when page is not rendered. when i hit ...
4
votes
2
answers
9k
views
What is the suitable type for renderHook in react-testing-library and TypeScript?
I wanna test a custom hook with react-testing-library therefore, I add this code into beforeEach:
let renderedHook ;
beforeEach(() => {
renderedHook = renderHook(() => useFetch());
});
test('...
5
votes
2
answers
10k
views
Testing hooks which throw errors
The [deprecated?] react-hooks-testing-library would return any errors thrown by the hook under test.
Probably my misunderstanding, but it looks like implementation now in the main @testing-library/...
0
votes
1
answer
198
views
Assert that a function changes a stateful value
So, here is a simplified version of my code.
This is my custom hook.
export const useStep = () => {
const [step, setStep] = useState<Steps>("sending");
const changeStep = (...
1
vote
0
answers
2k
views
Formik useFormikContext testing with testing-library
I am trying to write some unit tests for my input components, most of them are connected to Formik and there are cases like my Autocomplete component where the form gets validated when the user ...
0
votes
1
answer
201
views
`@testing-library/react-hooks` is loading my whole project?
I think this is an issue with module resoltuion/babel/typescript.
When using @testing-library/react-hooks, I'm running into a strange stack:
FAIL src/hooks/useAllPayouts.test.tsx
● Test suite ...
1
vote
1
answer
798
views
Testing React Native Image.getSize: Cannot spyOn on a primitive value; string given
I have a hook that detects the orientation of a React Native Image:
import { useState, useEffect } from 'react'
import { Image } from 'react-native'
const useFindImageSize = (image) => {
const [...
2
votes
2
answers
2k
views
How to test that dispatch only gets called once
I have a custom hook that dispatches an action when a URL parameter changes:
export const useUser = (): void => {
const dispatch = useDispatch();
const { user } = useParams<{ user: string }&...
0
votes
1
answer
3k
views
Unit testing a custom hook to ensure that it calls another hook
How can we ensure that a custom hook actually calls a method exposed by another hook?
Let's say, I have a custom hook useName that internally leverages useState.
import { useState } from 'react'
...
8
votes
1
answer
5k
views
When to use waitForNextUpdate rather than act + jest.advanceTimersByTime?
There is an example on advanced-hooks#async doc.
I am confused about how does waitForNextUpdate works. I made two test cases to compare waitForNextUpdate and act() + jest.advanceTimersByTime().
index....
2
votes
1
answer
1k
views
Destructuring need waitFor
Why does destructuring need waitFor?
Codesandbox
All tests do the same thing. In the 'ok.test.ts' file I use renderHook, I use result.current[1] to set the state and result.current[0] to get the ...
2
votes
1
answer
2k
views
The current result of a custom hook is updated when testing using react-hooks-testing-library
I am testing the custom React Hook shown below. Its functionality is to get the size of an image and then use the props provided to calculate the size of the image that the user wants.
import { ...
1
vote
1
answer
860
views
Get Firebase Error: No Firebase App '[DEFAULT]' has been created when using React-hooks-testing-library with jest
I using Firebase auth in React, and I try to test it with react-hooks-testing-library. The code I write is working. But when I try to test with react-hooks-testing-library I get this error:
...
2
votes
1
answer
2k
views
How can we test a customHook with useEffect with no return values inside by triggering it multiple times?
Hi all I am new to jest and now working on some tests for our customHooks.
My customHook has useEffect inside and does not return value:
const useCustomHook = (func: EffectCallback, deps?: ...
1
vote
1
answer
3k
views
Testing async custom hooks that useEffect with react-hooks-testing-library
I've written a simple React hook and want to test it with react-hooks-testing-library.
This hook calls an async function once both provider and domain variables, then once it's resolved puts data in ...
2
votes
1
answer
6k
views
How to use react-testing-library and jest with mocked custom react hook updating?
Look at the following custom hook. The premise is that it updates its state when query changes.
export function UseCustomHook() {
const { query } = useRouter()
const [state, setState] = useState({}...
0
votes
0
answers
378
views
Get State from React Hook Testing
I'm having a problem trying to access state and setState using react-hooks-testing-library.
I've followed examples from various sources using Provider but it doesn't work
Pay.tsx
export const Pay = ():...
3
votes
1
answer
3k
views
React Testing Library - testing hooks with React.context
I have question about react-testing-library with custom hooks
My tests seem to pass when I use context in custom hook, but when I update context value in hooks cleanup function and not pass.
So can ...
1
vote
1
answer
4k
views
testing callback return value with react-hooks-testing-library
In this example we have a simple hook called useLog that returns a method. How do I test that it gets returned with react-hooks-testing-library. I am trying to figure out how to write the expect.
The ...