0

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" } 
}); 
    }); 
}); 
+0

이'객체를 생성하는 class' 구문을 남용하지 마십시오 - 정적 메소드가있는 클래스를 작성하지! 그리고 모듈 개체를 기본으로 내보내는 대신 여러 개의 이름이 지정된 내보내기를 사용하는 것이 더 좋습니다 – Bergi

+0

왜 정적 기능에 클래스를 사용하지 않는 것이 좋을까요? 저는 js 개발을 처음 사용했습니다 – loganathan

+0

모든 오버 헤드로 인해. 다른 언어와 달리 클래스는 중앙 빌딩 블록이 아니라 객체를 인스턴스화 할 때만 사용해야하는 기능입니다. – Bergi

답변

0

문제는 fetch가 약속을 반환한다는 것입니다. 그 때문에 당신은 (docs)를 테스트에서 약속을 반환하거나 async/await를 사용하는 하나 있습니다

import Request from '../../scripts/lib/request'; 
const result = { 
    "1": { 
    "id": '1', 
    "text": "Read description of programming challenge" 
    }, 
    "2": { 
    "id": "2", 
    "text": "Implement awesome web app" 
    }, 
    "3": { 
    "id": "3", 
    "text": "Polish project" 
    }, 
    "9": { 
    "id": "9", 
    "text": "Send solution to LogMeIn" 
    } 
}); 
describe('request',() = > { 
    it('Should return all tasks', function() { 
    var allTasks = Request.get("api/tasks"); 
    return get.then(() = > 
     expect(allTasks).toEqual(result); 
    ) 
    }); 
}); 
describe('request',() = > { 
    it('Should return all tasks', async 
    function() { 
     var allTasks = await Request.get("api/tasks"); 
     expect(allTasks).toEqual(result); 
    }); 
}); 
+0

몇 가지 유용한 테스트 사례를 공유 할 수 있습니까? – loganathan

0
jest.dontMock('../scripts/lib/request'); 
import Request from '../scripts/lib/request'; 
const fs = require('fs') 
Request.get = jest.genMockFn(); 
Request.get.mockImplementation(function(url) { 
    let data = { 
     "1": { "id": '1', "text": "Read description of programming challenge" }, 
      "2": { "id": "2", "text": "Implement awesome web app" }, 
      "3": { "id": "3", "text": "Polish project" }, 
     }; 
return Promise.resolve(data); 
}); 
export default Request; 
관련 문제