2

I am trying to run my first Jest test but I get this error

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

    ReferenceError: sessionStorage is not defined

I'm not sure if I should be even getting this error as I am not testing sessionStorage, just want to test the root container.

--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)

I am "using" sessionStorage via a library, oidc-clientjs, so I really don't have control over it.

Line 23 that it is the source of the error is

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

2 Answers 2

5

I am using Create-react-app, so I added this to src/setupTests.js

const sessionStorageMock = {
  getItem: jest.fn(),
  setItem: jest.fn(),
  clear: jest.fn()
};
global.sessionStorage = sessionStorageMock;
1

The easiest way is to mock out redux-oidc. There are 2 ways to do so.

You need createUserManager to behave differently on some tests:

//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 does the same in some tests:

//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)
})
7
  • 1
    Could you please post the code that is using sessionStorage as well? Dec 5, 2016 at 8:27
  • thank you! one final question, issue isn't 100% resolved as I get sessionStorage.getItem is not defined. I have to mock getItem as well? Best way to do it?
    – chefcurry7
    Dec 5, 2016 at 8:57
  • Thats strange cause by mocking redux-oidc it should never call any method inside of redux-oidc. Or is sessionStorage.getItem not called inside redux-oidc but in your module? Then you need to follow the approach I showed first. Dec 5, 2016 at 9:02
  • yep i am calling session.getItem in my application code. alright thanks
    – chefcurry7
    Dec 5, 2016 at 9:03
  • i'd appreciate an examppe :)) i am very new to this. I tried sessionStorage.getItem.mockImplementation(() => 'test1234') :S
    – chefcurry7
    Dec 5, 2016 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.