Jest

    react-redux mock example

    react-redux 모듈을 모킹하기 위해 '/__mocks__' 디렉토리를 생성하고 'react-redux.js'를 생성한다. // path: /__mocks__/react-redux.js export const useDispatch = jest.fn(); export const useSelector = jest.fn((selector) => selector({})); 테스트의 대상이 되는 컴포넌트는 아래와 같다. import { useDispatch, useSelector } from 'react-redux'; import { updateCheckedItem } from '../actions'; export default function App() { const dispatch = useDispatc..

    Jest fetch mock example

    // api.test.js import { fetchWeather, fetchLocation, } from './api'; import WEATHER from '../../fixtures/weather'; import LOCATION from '../../fixtures/location'; describe('api', () => { const mockFetch = (data) => { global.fetch = jest.fn().mockResolvedValue({ async json() { return data; }, }); }; describe('fetchWeather', () => { beforeEach(() => { mockFetch(WEATHER); }); it('returns weather', ..

    [jest] The error below may be caused by using the wrong test environment

    The error below may be caused by using the wrong test environment, see https://jestjs.io/docs/configuration#testenvironment-string. Consider using the "jsdom" test environment. ReferenceError: document is not defined 11 | const handleClick = jest.fn(); 12 | > 13 | const { container, getByText } = render(( | ^ 14 | > testEnvironment: 'jest-environment-jsdom' 하지만 굳이 아래와 같이 설정하지 않아도 잘 돌아가는 경우도 있었는데..