amazon-archives/amazon-cognito-js

ReferenceError: window is not defined

exabugs opened this issue · 5 comments

On plain JS (not Browser) environment (ex. unit test), CognitoSyncManager don't work properly.

ReferenceError: window is not defined
    at new a (src/_lib/amazon-cognito.min.js:35:23975)
    at new a (src/_lib/amazon-cognito.min.js:35:13090)
    at new AWS.CognitoSyncManager (src/_lib/amazon-cognito.min.js:35:377)
    at server/server_test.js:43:28
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickDomainCallback (internal/process/next_tick.js:122:9)

code is below.

      aws.config.credentials.get(() => {
        const syncClient = new aws.CognitoSyncManager();
        syncClient.openOrCreateDataset('myDataset', (err, dataset) => {
          dataset.put('myKey', 'myValue', (err, record) => {
            dataset.synchronize({
              onSuccess: (data, newRecords) => {
                // Your handler code here
                console.log();
              }
            });
          });
        });
      });

What JS environment are you using? In nodeJS there is no window object.

Yes, I use this on Nodejs6.9 + Mocha at UnitTest environment.
Production environment is on the modern browser, Babel + Webpack compiled JS.

Test with browser is too difficult, so I'd like to test it on simple environment.

This library is wrapper HTML5 WebStorage and only use on Browser, isn't it ?

Yes, it uses window local storage for storing data. That wouldn't be available in node.

So what can be used instead of window? Can we make it work with node?

You could use a package that substitutes that in node such as node-localstorage. However, we have only tested this library in a browser environment.

https://www.npmjs.com/package/node-localstorage