jsonld.js icon indicating copy to clipboard operation
jsonld.js copied to clipboard

ReferenceError: window is not defined in Protractor / E2E Tests

Open tobiasschweizer opened this issue 4 years ago • 1 comments

I am developing an Angular library that has an indirect dependency on jsonld. When I run my E2E tests that import elements of my Angular library I get:

E/launcher - Error: ReferenceError: window is not defined at Object. (/.../node_modules/jsonld/dist/jsonld.js:10:4)

I found out that jsonld/dist/jsonld.js (distributed code: https://www.npmjs.com/package/jsonld/v/1.8.1) uses window: (window, function() { (line 10).

My understanding is that protractor is not aware of DOM objects that are normally available in client-side JS, so I added the following code in protractor.conf.js:

global['window'] = {
    addEventListener: () => {}
};

Details can be found here: https://github.com/dasch-swiss/dsp-ui-lib/pull/124

Has anyone faced a similar issue with protractor and jsonld? If yes, how was it solved?

tobiasschweizer avatar Jul 17 '20 10:07 tobiasschweizer

I am still using this workaround to import jsonld: const jsonld = require('jsonld/dist/jsonld.js');

I tried to change this to import * as jsonld from 'jsonld'; but then I get

jsonld.js:1034 Uncaught ReferenceError: global is not defined at wrapper (jsonld.js:1034) at Object../node_modules/jsonld/lib/jsonld.js (jsonld.js:1073)

tobiasschweizer avatar Jul 17 '20 12:07 tobiasschweizer