Closed
Bug 1141698
Opened 10 years ago
Closed 10 years ago
sessionStorage and localStorage are difficult to mock for test purposes
Categories
(Core :: DOM: Core & HTML, defect)
Core
DOM: Core & HTML
Tracking
()
RESOLVED
INVALID
People
(Reporter: Irving, Unassigned)
Details
Under Webkit, sessionStorage and localStorage methods (e.g. getItem) can be overridden by test frameworks. On Firefox, attempts to override individual methods in sessionStorage and localStorage fail. It is possible to completely mock out window.sessionStorage and window.localStorage, but that is inconvenient...
See:
https://github.com/cjohansen/Sinon.JS/issues/662
https://github.com/jasmine/jasmine/issues/299
http://twofuckingdevelopers.com/2014/07/solving-spyon-problems-in-jasmine/
Comment 1•10 years ago
|
||
Per spec, sessionStorage and localStorage have a named setter. Performing sets on them will invoke this setter, no?
So for example:
sessionStorage.foo = 5;
alert(sessionStrage.getItem("foo"));
is supposed to alert 5.
Now consider this code:
sessionStorage.clear = 5;
alert(sessionStorage.getItem("clear"));
Per spec, this is supposed to behave just like the "foo" case. But in Chrome, at least, it doesn't; instead of invoking the named setter the set just defines a property on the sessionStorage object itself. That's totally not what the spec says to do.
As far as I can tell, that's the behavior difference you're running into.
What you probably want to do, in any case, is to override the methods on Storage.prototype instead of trying to override them on the instance objects themselves.
Status: NEW → RESOLVED
Closed: 10 years ago
Resolution: --- → INVALID
| Reporter | ||
Comment 2•10 years ago
|
||
Overriding Storage.prototype is less focused than I'd like for a test; I'll look at other ways to mock out localStorage. That said, the gray area I see in the spec is:
sessionStorage.clear = '5';
alert(sessionStorage.clear);
As I read it, the key/value pair of Storage data gets mapped to an object property so I'd expect the alert to display '5', but now how do I get to the Storage.clear() method?
Comment 3•10 years ago
|
||
> sessionStorage.clear = '5';
> alert(sessionStorage.clear);
This will alert the original clear function, because Storage is not [OverrideBuiltins].
> the key/value pair of Storage data gets mapped to an object property
Sort of. There are some interesting interactions with the prototype chain.
I really wish the people who'd originally implemented Storage had just stuck with setItem/getItem instead of trying to stick an arbitrary-string-key store on an object with string-named methods....
| Assignee | ||
Updated•6 years ago
|
Component: DOM → DOM: Core & HTML
You need to log in
before you can comment on or make changes to this bug.
Description
•