Mocha: How to mock session storage when running a test?

Created on 9 May 2016  路  4Comments  路  Source: mochajs/mocha

I am trying to setup mocha for my reactjs app.
However, running the command mocha --compilers js:babel-register --recursive produces an error saying

mocha --compilers js:babel-register --recursive

/Users/purezen_/workspace/webapp/src/js/data/reducers/auth.js:31
var auth = sessionStorage.getItem('auth');
           ^

ReferenceError: sessionStorage is not defined
    at Object.<anonymous> (auth.js:5:14)
    at Module._compile (module.js:397:26)
    at loader (/Users/purezen_/workspace/kredx-fe/client/node_modules/babel-register/lib/node.js:158:5)

even when I have included code to mock session storage inside the test file according to http://stackoverflow.com/q/11485420/1509855

Most helpful comment

All 4 comments

This was a lifesaver! Thanks a ton @purezen
I used mock-local-storage to mock my sessionStorage. It was as simple as just installing with npm and running the test. No additional code needs to be written!!

How to do this?
It is not working for me.
Please help

Do I need to change any file?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Almad picture Almad  路  41Comments

ghost picture ghost  路  32Comments

ibc picture ibc  路  59Comments

teckays picture teckays  路  84Comments

sagiegurari picture sagiegurari  路  61Comments