mardi 31 mai 2016

ReferenceError: fetch is not defined in redux application testing with Mocha

I testing my React-Redux application with Mocha. When I covering async action creators and run npm start I have an error from Mocha

ReferenceError: fetch is not defined;

My Test code:

import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import * as activity from '../../app/actions/activity'
import * as admin from '../../app/actions/admin'
import nock from 'nock'
import {expect} from 'chai'

const middlewares = [ thunk ]
const mockStore = configureMockStore(middlewares)

describe('async functions', () => {
afterEach(() => {
    nock.cleanAll()
})

it('creates FETCH_ACTIVITY_SUCCESS when fething activities has been done', () => {
    nock('http://example.com')
    .get('/dashboard/activity')
    .reply(200, {body : {activityList: [{"label":"Data uploaded","description":" igunchenko@griddynamics.com","activityDate":{"hour":17,"minute":18,"second":28,"nano":0,"dayOfYear":145,"dayOfWeek":"TUESDAY","month":"MAY","dayOfMonth":24,"year":2016,"monthValue":5,"chronology":{"id":"ISO","calendarType":"iso8601"}}}]}})

    var expectedActions = [
        {type: activity.REQUEST_ACTIVITY},
        {type: activity.RECEIVE_ACTIVITY}
    ]
    var store = mockStore({activityList: []})
    return store.dispatch(activity.fetchActivity())
    .then(() => {
        expect(store.getActions()).toEqual(expectedActions)
    })
})

it('creates FETCH_ADMIN_SUCCESS when fething activities has been done', () => {
    nock('http://example.com')
    .get('/customers')
    .reply(200, {body : {customers: [{"customerId":1,"accountId":24,"active":true,"confirmed":true,"priority":5,"email":"nusateno@griddynamics.com","fullName":"Nikita","company":"Nikita","industry":"Others","phone":"","admin":true},{"customerId":4,"accountId":27,"active":true,"confirmed":false,"priority":5,"email":"1@1.ru","fullName":"The fifth great user","company":"RA test team","industry":"Software, High-Tech","phone":"3537375477","admin":true}]}})

    var expectedActions = [
        {type: admin.REQUEST_CLIENT_LIST},
        {type: admin.RECEIVE_CLIENT_LIST}
    ]

    var store = mockStore({customers: []})
    return store.dispatch(admin.fetchClientList())
    .then(() => {
        expect(store.getActions()).toEqual(expectedActions)
    })
})
})

After this exception I have added

import fetch from 'whatwg-fetch'

and got this:

ReferenceError: self is not defined

How I can solve this problem?

Aucun commentaire:

Enregistrer un commentaire