ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • expect.anything()
    테스팅/JEST 2019. 10. 21. 12:53

    anything()은 null이나 undefined을 제외한 모든 값들과 일치한다.

    이게 무슨 말이냐면 null, undefined외의 모든 값과 비교했을 때 동일하다는 결과를 기대한다는 것이다.

     

    이는 null, undefined를 제외한 모든 값의 상태를 허용할 때 사용한다.

     

    # 리터럴 값

    아래의 코드를 보자.

    test('map calls its argument with a non-null argument', () => {
        const mock = jest.fn();
    
        [1].map(x => mock(x));
    
        expect(mock).toBeCalledWith(1);
    });

     

    길이가 1인 배열에 map()을 통해 익명함수를 정의했다.

    익명함수 안에서 mock()을 호출하는 코드를 작성했다.

    만약 이것이 실행된다면 mock의 호출 횟수는 1일 것이다.



     

    결과를 확인하면 테스트에 성공하는 것을 볼 수 있다.

     

    그럼 이제 실패하는 케이스를 만들어보자.

    toBeCalledWith()에 인자로 2를 넣고 테스트를 돌려보자.

    test('map calls its argument with a non-null argument', () => {
        const mock = jest.fn();
    
        [1].map(x => mock(x));
    
        // expect(mock).toBeCalledWith(1);
        expect(mock).toBeCalledWith(2);
    });
    

     

     

    예상 결과는 2였는데 실제 받은 값은 1이기에 테스트에 실패했다.

    이는 아주 정상적인 결과다.

     

    그럼 mock()이 몇번 호출 되든 상관없이 테스트 케이스에 성공으로 처리하려면 어떻게 해야할까?

    그 때 바로 anything()을 사용하는 것이다.

     

    아래와 같이 expect.anything()을 toBeCalledWith() 안에 넣고 테스트를 실행해보자.

     

    test('map calls its argument with a non-null argument', () => {
        const mock = jest.fn();
    
        [1].map(x => mock(x));
    
        // expect(mock).toBeCalledWith(1);
        // expect(mock).toBeCalledWith(2);
        expect(mock).toBeCalledWith(expect.anything());
    });
    

     

     

    expect.anything()은 1이 아니다. 

    그럼에도 테스트 결과에는 통과되었다.

     

    이는 즉 호출 횟수가 몇번이 되든 상관없이 null과 undefined만 아니라면 통과시킨다는 이야기다.

     

     

     

    # 객체비교

    이는 객체를 비교할 때에도 유용하다.

    아래의 코드를 작성하자.

    test('Math와 Kor는 같다.', () => {
        class Math {
            constructor(one, two, three) {
                this.one = one;
                this.two = two;
                this.three = three;
            }
        };
    
        class Kor {
            constructor(one, two, three) {
                this.one = one;
                this.two = two;
                this.three = three;
            }
        };
    
        const korObj = new Kor('하나', '둘', '셋');
        const mathObj = new Math(1, 2, 3);
    
        expect(korObj).toEqual(mathObj);
    });

     

    Math라는 클래스와 Kor라는 클래스를 만들고 각각 객체를 생성한다.

    그리고 toEqual()을 통해 같은 객체인지 비교한다.

     

     

    테스트를 돌려보면 당연히 실패가 발생한다.

     

     

    이럴 때 anything()을 사용하면 어떻게 될까?

    test('Math와 Kor는 같다.', () => {
        class Math {
            constructor(one, two, three) {
                this.one = one;
                this.two = two;
                this.three = three;
            }
        };
    
        class Kor {
            constructor(one, two, three) {
                this.one = one;
                this.two = two;
                this.three = three;
            }
        };
    
        const korObj = new Kor('하나', '둘', '셋');
        const mathObj = new Math(1, 2, 3);
    
        // expect(korObj).toEqual(mathObj);
        expect(korObj).toEqual(expect.anything());
    });

     

     

    이 경우에도 모든 케이스를 통과시킨다.

     

     

    # 결론

    anything()은 null이나 undefined를 제외한 모든 값을 허용하는 테스트에서 사용하면 된다.

    '테스팅 > JEST' 카테고리의 다른 글

    Mock Return Value  (0) 2019.10.29
    Mock Property  (0) 2019.10.29
    Mock Function  (0) 2019.10.29
    expect.extend()  (0) 2019.10.18
    기초  (0) 2019.10.18
Designed by Tistory.