Jest & Jest 모형을 사용하여 ES6 앱의 테스트 케이스를 작성하려고합니다. 그러나 모형은 테스트 스위트에 의해 선택되지 않습니다. 누군가가 나를Jest로 ES6 가져 오기 메소드를 테스트하는 방법
을 테스트 할 수있는 적절한 방법을 알려 수있는 것은
class Request{
//
// Set Header for All the requests
//
static get HEADERS() {
return {
"Accept": "application/json, text/plain",
"Content-Type": "application/json"
};
}
//
// GET Request
//
static get(url){
return fetch(url)
.then(response => {
if (!response.ok) {
throw new Error(response.statusText);
}
return response.json();
})
.catch(err => {
console.log(err);
});
}
}
request.js // 농담-모형을
import configureMockStore from 'redux-mock-store' // mock store
import thunk from 'redux-thunk'
const middlewares = [ thunk ]
const mockStore = configureMockStore(middlewares)
const tasks = {
"1": { "id": '1', "text": "Read description of programming challenge" },
"2": { "id": "2", "text": "Implement awesome web app" },
"3": { "id": "3", "text": "Polish project" }
};
import Request from '../scripts/lib/request';
describe('Request',() => {
it('List all task fetch request',() => {
console.log("11111");
fetch.mockResponses(JSON.stringify(tasks));
const expectedActions = [{ type: 'SET_TASKS', tasks}];
const store = mockStore(tasks);
return store.dispatch(store.get())
.then(() => { // return of async actions
expect(store.getActions()).toEqual(expectedActions)
})
}
}
request.spec.js을 request.js // 단위 테스트
import Request from '../../scripts/lib/request';
describe('request',() => {
it('Should return all tasks', function() {
var allTasks = Request.get("api/tasks");
expect(allTasks).toEqual({
"1": { "id": '1', "text": "Read description of programming challenge" },
"2": { "id": "2", "text": "Implement awesome web app" },
"3": { "id": "3", "text": "Polish project" }
});
});
});
이'객체를 생성하는 class' 구문을 남용하지 마십시오 - 정적 메소드가있는 클래스를 작성하지! 그리고 모듈 개체를 기본으로 내보내는 대신 여러 개의 이름이 지정된 내보내기를 사용하는 것이 더 좋습니다 – Bergi
왜 정적 기능에 클래스를 사용하지 않는 것이 좋을까요? 저는 js 개발을 처음 사용했습니다 – loganathan
모든 오버 헤드로 인해. 다른 언어와 달리 클래스는 중앙 빌딩 블록이 아니라 객체를 인스턴스화 할 때만 사용해야하는 기능입니다. – Bergi