본문 바로가기
728x90

전체 글368

TestCode: 유닛 테스트 | Jest - Mock Names 로 테스트 함수 식별 jest.fn() 사용 시 선택적으로 테스트 오류 출력 대신 모의 함수의 이름을 제공할 수 있다 .mockName() 을 사용하여 테스트 출력에서 오류를 보고하는 모의 함수를 빠르게 식별할 수 있다 const myMockFn = jest .fn() .mockReturnValue('default') .mockImplementation(scalar => 42 + scalar) .mockName('add42'); 2023. 7. 14.
TestCode: 유닛 테스트 | Jest - Mock Implementations 로 복잡한 동작 모의 테스트 모의 구현 jset.fn 은 모의 함수 또는 메서드를 mockImplementationOnce 를 사용하여 수행할 수있다 mockImplementationOnce 메서드는 다른 모듈에서 생성된 모의 함수의 기본 구현을 정의해야 할 때 유용하다 const myMockFn = jest.fn(cb => cb(null, true)); myMockFn((err, val) => console.log(val)); // > true // foo export function () { // some implementation; }; // test jest.mock('../foo'); // this happens automatically with automocking import foo from '../foo' // foo.. 2023. 7. 13.
TestCode: 유닛 테스트 | Jest - Mocking Partials 로 모듈의 일부만 모의 테스트 Mocking Partials 모듈의 일부만 mocking 하여 테스트 할 수 있다 // foo-bar-baz export const foo = 'foo'; export const bar = () => 'bar'; export default () => 'baz'; import defaultExport, {bar, foo} from '../foo-bar-baz'; jest.mock('../foo-bar-baz', () => { const originalModule = jest.requireActual('../foo-bar-baz'); //Mock the default export and named export 'foo' return { __esModule: true, ...originalModule, de.. 2023. 7. 13.
TestCode: 유닛 테스트 | Jest - Mocking Module 로 모듈에 대한 모의 테스트 Mocking Module 모듈에 대해 Mocking 하는 것 API 호출 등의 모듈을 실제로 동작하지 않고 테스트를 한다 jset.mock(...) 함수를 사용하여 모듈을 모의 테스트 할 수 있다 모듈을 모킹하면 테스트에서 반환 값을 지정할 수 있다 모의 응답을 반환하는 것 test import axios from 'axios'; // test 할 모듈 class Users { static all() { return axios.get('/users.json').then(resp => resp.data); } } export default Users; import axios from 'axios'; import Users from './users'; jest.mock('axios'); test('shou.. 2023. 7. 13.
728x90