Skip to content
← Back to rules

jest/prefer-called-with 스타일

An auto-fix is available for this rule.

작동 방식

toBeCalledWith() 또는 toHaveBeenCalledWith() 사용을 제안합니다.

왜 문제가 되는가?

함수 호출을 테스트할 때, 함수가 호출되었는지와 함께 어떤 인자를 사용해서 호출되었는지 검증하는 것이 더 중요합니다.
toBeCalled() 또는 toHaveBeenCalled()를 사용하면 함수가 호출되었는지만 확인할 수 있으며, 인자 검증은 이루어지지 않아 잘못된 매개변수로 함수가 호출되는 버그를 놓칠 수 있습니다.

예시

이 규칙에 위배되는 잘못된 코드 예시:

javascript
expect(someFunction).toBeCalled();
expect(someFunction).toHaveBeenCalled();

이 규칙에 부합하는 올바른 코드 예시:

javascript
expect(noArgsFunction).toBeCalledWith();
expect(roughArgsFunction).toBeCalledWith(expect.anything(), expect.any(Date));
expect(anyArgsFunction).toBeCalledTimes(1);
expect(uncalledFunction).not.toBeCalled();

이 규칙은 eslint-plugin-vitest와 호환됩니다. 이를 사용하려면 .oxlintrc.json에 다음 설정을 추가하세요:

json
{
  "rules": {
    "vitest/prefer-called-with": "error"
  }
}

사용 방법

이 규칙을 설정 파일이나 명령줄에서 활성화하려면 다음을 사용할 수 있습니다:

json
{
  "plugins": ["jest"],
  "rules": {
    "jest/prefer-called-with": "error"
  }
}
bash
oxlint --deny jest/prefer-called-with --jest-plugin

참고 자료