TIL | Jest 자주 사용하는 메서드 정리

bubblegum·2024년 2월 28일
0

Today I learn(TIL)

목록 보기
28/84
post-thumbnail

알겠습니다. Jest에서 자주 사용되는 다른 메서드들도 포함하여 예시를 보여드리겠습니다.

  1. test(name, fn, timeout):

    // 예시:
    test('1 더하기 2는 3이다', () => {
        expect(1 + 2).toBe(3);
    });
  2. expect(value):

    // 예시:
    expect(1 + 2).toBe(3);
  3. beforeEach(fn):

    // 예시:
    let value;
    beforeEach(() => {
        value = 10;
    });
    test('값이 10인지 확인', () => {
        expect(value).toBe(10);
    });
  4. afterEach(fn):

    // 예시:
    let value;
    afterEach(() => {
        value = null;
    });
    test('값이 null인지 확인', () => {
        expect(value).toBeNull();
    });
  5. describe(name, fn):

    // 예시:
    describe('add 함수', () => {
        test('1 + 2는 3이다', () => {
            expect(1 + 2).toBe(3);
        });
        test('2 + 2는 4이다', () => {
            expect(2 + 2).toBe(4);
        });
    });
  6. toEqual(value):

    // 예시:
    const obj = { name: 'John' };
    expect(obj).toEqual({ name: 'John' });
  7. toBe(value):

    // 예시:
    expect(1 + 2).toBe(3);
  8. toThrow(error):

    // 예시:
    function throwError() {
        throw new Error('예외 발생');
    }
    expect(throwError).toThrow(Error);
  9. jest.fn([implementation]):

    // 예시:
    const mockFunc = jest.fn();
    mockFunc.mockReturnValue('mocked value');
    mockFunc(); // 'mocked value'를 반환
  10. jest.mock(moduleName, factory, options):

    // 예시:
    jest.mock('./utils');
    // './utils' 모듈을 모의화하여 필요에 따라 구현을 정의
  11. jest.spyOn(object, methodName):

    // 예시:
    const object = {
        method: () => 'original implementation'
    };
    jest.spyOn(object, 'method');
    object.method.mockReturnValue('mocked value');
    object.method(); // 'mocked value'를 반환
  12. mockReturnValue(value):

    // 예시:
    const mockFunc = jest.fn();
    mockFunc.mockReturnValue('mocked value');
    mockFunc(); // 'mocked value'를 반환
  13. mockResolvedValue(value):

    // 예시:
    const mockAsyncFunc = jest.fn();
    mockAsyncFunc.mockResolvedValue('resolved value');
    await mockAsyncFunc(); // Promise.resolve('resolved value')를 반환
  14. mockRejectedValue(value):

    // 예시:
    const mockAsyncFunc = jest.fn();
    mockAsyncFunc.mockRejectedValue(new Error('rejected value'));
    await mockAsyncFunc(); // Promise.reject(new Error('rejected value'))를 반환
  15. mockImplementation(implementation):

    // 예시:
    const mockFunc = jest.fn();
    mockFunc.mockImplementation((a, b) => a + b);
    mockFunc(2, 3); // 5를 반환
  16. mockResolvedImplementation(implementation):

    // 예시:
    const mockAsyncFunc = jest.fn();
    mockAsyncFunc.mockResolvedImplementation(() => Promise.resolve('resolved value'));
    await mockAsyncFunc(); // Promise.resolve('resolved value')를 반환
  17. mockRejectedImplementation(implementation):

    // 예시:
    const mockAsyncFunc = jest.fn();
    mockAsyncFunc.mockRejectedImplementation(() => Promise.reject(new Error('rejected value')));
    await mockAsyncFunc(); // Promise.reject(new Error('rejected value'))를 반환
profile
황세민

0개의 댓글

관련 채용 정보