Jest & Jestモックアップを使用してES6アプリケーションのテストケースを作成しようとしています。しかし、モックアップはテストスイートでは一切選択されていません。誰かが私にJestでES6のフェッチメソッドをテストする方法
request.js
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をテストするための適切な方法を知らせることができます //ユニットテスト
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