[ 
https://issues.apache.org/jira/browse/FLAGON-451?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16938140#comment-16938140
 ] 

Rob Foley commented on FLAGON-451:
----------------------------------

So the issue has nothing to do with jsdom and everything to do with how 
getInitialSettings has been refactored. The error is occurring _before_ any 
tests actually run. The error occurs as part of the "import" step when 
importing "getInitialSettings.js". This is because of the line:

 
{code:java}
var SessionId = getSessionId("userAleSessionId", 'session_' + 
String(Date.now()));
{code}
This line runs _immediately_ as soon as the module is imported, which means it 
runs both in the jsdom tests as well as in the test harness itself. Obviously 
there is no window or sessionStorage object in the node environment, so it 
fails.

 

Additional evidence of this is that the use of dom-storage is incorrect. If you 
try to run:
{code:java}
import { Storage } from 'dom-storage';

new Storage(null, { strict: true });
{code}
you'll see that Storage isn't supposed to be imported that way. The only reason 
an error hasn't cropped up is because it isn't reaching the tests at all.

 

A few side issues exist as well:

First: we can't see any errors/log output from the script running _inside_ the 
jsdom environment. This is fixed by adding a "VirtualConsole" that maps the 
jsdom output to the node console. I added this and started seeing errors 
pertaining to missing sessionStorage (despite it being correctly set in the 
tests).

Second: we can't mock window.sessionStorage because the "done" callback for 
jsdom runs _after_ the HTML has been parsed and all scripts have had a chance 
to execute. This means UserALE has already tried (and failed) to initialize 
itself from sessionStorage. To fix this I added a "created" callback that 
attaches the dom-storage object _before_ the UserALE script gets a chance to 
run. Doing this fixed all of the errors and the tests are now passing.

Third: There are some semantic/stylistic errors in getInitialSettings. They 
have been corrected.

> Update unit tests to accommodate session storage features
> ---------------------------------------------------------
>
>                 Key: FLAGON-451
>                 URL: https://issues.apache.org/jira/browse/FLAGON-451
>             Project: Flagon
>          Issue Type: Sub-task
>          Components: UserALE.js
>    Affects Versions: UserALE.js 2.1.0
>            Reporter: Joshua Poore
>            Assignee: Joshua Poore
>            Priority: Major
>             Fix For: UserALE.js 2.1.0
>
>
> session storage needs to be a known object in unit tests.
> exercising sessionStorage in getInitialSettings to preserve session Id at 
> page refresh (such as submit) performs expectedly in testing with the example 
> page, however breaks the following unit tests:
> {code:java}
>   1) getInitialSettings
>        getInitialSettings
>          fetches all settings from a script tag:
>      Uncaught TypeError: Cannot read property 'options' of undefined
>       at Object.done (test/getInitialSettings_spec.js:76:39)
>       at 
> /Users/jpoore/Documents/Apache_Flagon/test/incubator-flagon-useralejs/node_modules/jsdom/lib/jsdom.js:325:18
>       at processTicksAndRejections (internal/process/task_queues.js:75:11)
>   2) getInitialSettings
>        getInitialSettings
>          grabs user id from params:
>      Uncaught TypeError: Cannot read property 'options' of undefined
>       at Object.url [as done] (test/getInitialSettings_spec.js:95:39)
>       at 
> /Users/jpoore/Documents/Apache_Flagon/test/incubator-flagon-useralejs/node_modules/jsdom/lib/jsdom.js:325:18
>       at processTicksAndRejections (internal/process/task_queues.js:75:11)
>   3) Userale API
>        provides configs:
>      Uncaught TypeError: Cannot read property 'options' of undefined
>       at Object.done (test/main_spec.js:33:37)
>       at 
> /Users/jpoore/Documents/Apache_Flagon/test/incubator-flagon-useralejs/node_modules/jsdom/lib/jsdom.js:325:18
>       at processTicksAndRejections (internal/process/task_queues.js:75:11)
>   4) Userale API
>        edits configs:
>      Uncaught TypeError: Cannot read property 'options' of undefined
>       at Object.done (test/main_spec.js:58:37)
>       at 
> /Users/jpoore/Documents/Apache_Flagon/test/incubator-flagon-useralejs/node_modules/jsdom/lib/jsdom.js:325:18
>       at processTicksAndRejections (internal/process/task_queues.js:75:11)
>   5) Userale API
>        starts + stops:
>      Uncaught TypeError: Cannot read property 'options' of undefined
>       at Timeout._onTimeout (test/main_spec.js:75:24)
>       at listOnTimeout (internal/timers.js:531:17)
>       at processTimers (internal/timers.js:475:7)
>   6) Userale API
>        sends custom logs:
>      Uncaught TypeError: Cannot read property 'log' of undefined
>       at Object.done (test/main_spec.js:93:22)
>       at 
> /Users/jpoore/Documents/Apache_Flagon/test/incubator-flagon-useralejs/node_modules/jsdom/lib/jsdom.js:325:18
>       at processTicksAndRejections 
> (internal/process/task_queues.js:75:11){code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to