digitalbazaar / jsonld.js

A JSON-LD Processor and API implementation in JavaScript
https://json-ld.org/
Other
1.66k stars 195 forks source link

ReferenceError: window is not defined in Protractor / E2E Tests #405

Open tobiasschweizer opened 4 years ago

tobiasschweizer commented 4 years ago

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 commented 4 years ago

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)