GoJS in Mocha unit test with vue-cli-service

Related to Object Cannot read property 'defineProperties'

We are experiencing the same problems when we are trying to run mocha unit tests in an example set-up with vuejs3. The example set-up was created using the vue cli with the basic setting and after that running npm install --save gojs.

The example test:

import go from 'gojs';

describe('gojs test', () => {
    it('should run', () => {
        new go.Point(100, 200);
    });
});

throws the following error:

$ npm run test:unit

> vue-gojs-mocha-test-example@0.1.0 test:unit /home/rein/dev/vue-gojs-mocha-test-example
> vue-cli-service test:unit

 WEBPACK  Compiling...

  [=========================] 98% (after emitting)

 DONE  Compiled successfully in 1314ms

  [=========================] 100% (completed)

 WEBPACK  Compiled successfully in 1314ms

 MOCHA  Testing...

 RUNTIME EXCEPTION  Exception occurred while loading your tests

TypeError: Cannot read property 'defineProperties' of undefined
    at /home/rein/dev/vue-gojs-mocha-test-example/dist/js/webpack:/node_modules/gojs/release/go.js:21:1

We have looked at the issue for several hours but we cannot pinpoint why this is going wrong. Do you have any ideas?

You can reproduce the behavior as follows:

git clone https://github.com/eurogroep/vue-gojs-mocha-test-example.git
cd vue-gojs-mocha-test-example
npm ci
npm run test:unit

Thanks in advance!

The code is trying to use Object.defineProperties. The error seems to be saying that Object is not defined. I don’t know why that is. Is there a top-level value for window, this, global, or self?

Related question: Is it supposed to be running without a DOM?

Yes, this is supposed to be running without a dom. We haven’t found a way to test it this far.

Could you use a DOM?

I’ll have a look at this too and get back to you soon. Thanks for the reproducible case.

This is happening because we had assumed that any system which defined window would give it all the references that you might expect on a global object, such as window.Object. For some reason, in Mocha, window.Object does not exist (but Object, global.Object, and globalThis.Object do)

This will be fixed in the next release of GoJS.

Actually, it’s fixed right now in the GoJS 2.2 Alpha, if you want to start using the next version. You can install this with npm i gojs@alpha

The 2.1 release with this fix will be out in a few days.

Thanks a lot for all your efforts!

Updated the source to the alpha version and the problem seems to be fixed, thanks!