jeudi 18 mars 2021

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 'react'
export const useLog = () => {
  const log = useCallback(() => console.log("hello World"), [])
  return { log }
}

test:

import { renderHook } from '@testing-library/react-hooks'
import {useLog} from "./useCounter";

const log = jest.fn()

test('should increment counter', () => {
  const { result } = renderHook(() => useLog())

  expect(result.current.log).toHaveReturnedWith(log);
})

what I get:

Matcher error: received value must be a mock function
Received has type:  function
Received has value: [Function anonymous]

Aucun commentaire:

Enregistrer un commentaire