Questions about testing React hooks with the react-hooks-testing-library utility.
Questions tagged [react-hooks-testing-library]
90 questions
1
vote
1 answer
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/react lost this feature?
Here's what I'm…

Eric
- 935
- 1
- 8
- 23
1
vote
1 answer
How to mock useRouter parameters for react-hooks-testing-library?
I have a custom hook, which has structure of:
const urlHook = () => {
const router = useRouter();
const read = () => {
return validate(router.query.param);
}
const write = (params) => {
router.push(
{
query: {
param: params,
…

Alyona
- 1,682
- 2
- 21
- 44
1
vote
0 answers
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 presses special keys i.e ( "Enter", ",", " ", etc ),…

Sakis95
- 49
- 1
- 4
- 12
1
vote
1 answer
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 [width, setWidth] = useState(0)
const [height,…

bopbopbop
- 487
- 3
- 9
1
vote
0 answers
TypeError: Cannot read property 'hasOwnProperty' of undefined in renderHook import
I am trying to use renderHook from the testing-library.
When I run my test I get this error, TypeError: Cannot read property 'hasOwnProperty' of undefined
package.json
"devDependencies": {
"@babel/plugin-proposal-nullish-coalescing-operator":…
1
vote
2 answers
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 }>();
useEffect(() => {dispatch(getUser(user));
},…

Andy Jessop
- 225
- 1
- 13
1
vote
1 answer
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 { useEffect, useState } from 'react';
import { Image,…

pumpum
- 555
- 3
- 5
- 18
1
vote
1 answer
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:
FirebaseError: Firebase: No Firebase App '[DEFAULT]' has…

ken
- 2,426
- 5
- 43
- 98
1
vote
1 answer
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?: DependencyList) => {
const didMount = useRef(false);
…

Victor Cheng
- 102
- 1
- 7
1
vote
1 answer
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 state and returns it.
I couldn't find anywhere how…

v1rtl
- 185
- 3
- 8
1
vote
1 answer
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({})
useEffect(() => {
const updatedState =…

user10741122
- 781
- 1
- 12
- 26
1
vote
2 answers
waitFor times out after calling renderHook()
I am trying to test a simple custom hook:
export const getSearchResults = async (searchText: string) => {
const { data } = await axios.get(`${BASE_URL}/search`, {
params: {
searchText,
apiToken: API_KEY
},
});
return data…

Dániel Kocsis
- 11
- 1
- 2
1
vote
1 answer
Test custom hook with react-hooks-testing-library
I am trying to write tests for this custom hook using react-hooks-testing-library.
import { deleteArticleById } from '../../API/articles/deleteArticleById';
const useArticleDelete = () => {
const [articleId, setArticleId] = useState(null);
const…

mkonav
- 13
- 4
1
vote
1 answer
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 hooks useLog:
import { useCallback } from…

someRandomDude
- 11
- 1
- 3
1
vote
2 answers
how to test a hook with async state update in useEffect?
i have a simple hook that fetches the value and sets it to option as follows:
import Fuse from 'fuse.js'
import React from 'react'
// prefetches options and uses fuzzy search to search on that option
// instead of fetching on each keystroke
export…

Pravin
- 1,671
- 5
- 23
- 36