2016-12-05 3 views
0

내가 처음으로 농담 테스트를 실행하려고 정의되지하지만 난 심지어 내가 sessionStorage을 테스트하고 있지 않다으로이 오류가 발생 할 필요가있는 경우농담 테스트, sessionStorage는

FAIL src\containers\__test__\AppContainer.spec.js 
    ● Test suite failed to run 

    ReferenceError: sessionStorage is not defined 

는 잘 모르겠어요이 오류가 루트 컨테이너를 테스트하려고합니다.

--update--

import React from 'react' 
import { shallow } from 'enzyme' 
import AppContainer from '../AppContainer' 

//Tried here also 
global.sessionStorage = { 
    data: {}, 
    setItem: (key, value) => { 
    this.data[key] = value 
    }, 
    getItem: (key) => this.data[key] 
} 
describe('AppContainer',() => { 
    beforeEach(function() { 
    global.sessionStorage = { 
     data: {}, 
     setItem: (key, value) => { 
     this.data[key] = value 
     }, 
     getItem: (key) => this.data[key] 
    } 
    }) 

    it('should render self and subcomponents',() => { 
    const enzymeWrapper = shallow(<AppContainer />) 

    expect(enzymeWrapper.find('div').hasClass('grommetux-app')).toBe(true) 
    }) 
}) 

-

ReferenceError: sessionStorage is not defined 
    at Function.r.get (node_modules\oidc-client\lib\oidc-client.min.js:1:13009) 
    at new e (node_modules\oidc-client\lib\oidc-client.min.js:74:15382) 
    at new e (node_modules\oidc-client\lib\oidc-client.min.js:74:5255) 
    at n (node_modules\redux-oidc\dist\redux-oidc.js:1:1853) 
    **at Object.<anonymous> (src\utils\userManager.js:23:127)** 
    at Object.<anonymous> (src\containers\AppContainer.js:9:46) 
    at Object.<anonymous> (src\containers\__test__\AppContainer.spec.js:3:47) 
    at process._tickCallback (internal\process\next_tick.js:103:7) 

내가 도서관, oidc-clientjs를 통해 sessionStorage "을 사용하여"하고, 그래서 정말 제어 할 수 없습니다. 이 오류의 원인

23 행 가장 쉬운 방법은 redux-oidc을 조롱하는 것입니다

import { createUserManager } from 'redux-oidc' 
.... 
const userManager = createUserManager(config) (L23) 
+0

시도'window.sessionStorage'합니다. –

답변

1

입니다. 그렇게하는 데는 두 가지 방법이 있습니다.

당신은 몇 가지 테스트에서 다르게 행동 createUserManager 필요합니다

//you need to import the module as you need to set the behaviour of 
//createUserManager in every tests 
import { createUserManager } from 'redux-oidc' 

//mock the module with an object that just holds createUserManager  
//method as a simple spy, later in your tests you can define what this 
//spy should do 
jest.mock('redux-oidc',() => ({createUserManager: jest.fn()})) 

it('should render self and subcomponents',() => { 
    createUserManager.mockImplementation(() => 'test1234`)//set the mock implementation here 
    const enzymeWrapper = shallow(<AppContainer />) 
    expect(enzymeWrapper.find('div').hasClass('grommetux-app')).toBe(true) 
}) 

createUserManager는 몇 가지 테스트에서 동일한 않습니다 : 대신

//mock the module with an object that just holds createUserManager 
//method as a simple function that always returns 'test1234' 
jest.mock('redux-oidc',() => ({createUserManager:() => 'test1234'})) 

it('should render self and subcomponents',() => { 
    const enzymeWrapper = shallow(<AppContainer />) 
    expect(enzymeWrapper.find('div').hasClass('grommetux-app')).toBe(true) 
}) 
+0

감사합니다. 업데이트를 참조하십시오. 여전히 오류가 발생합니다 – chefcurry7

+0

'sessionStorage'도 사용하는 코드를 게시 해주십시오. –

+0

감사합니다. – chefcurry7