Error when instantiating go.Diagram in unit tests after upgrading to goJS v3

Hello,

we have a unit test which instantiates a go.Diagram in the beforeEach function:
diagram = new go.Diagram();

This worked in goJS v2
However, after upgrading to v3.0.11, the first test method for which the beforeEach is executed works, but all others fail with the following error:

TypeError: Cannot read properties of undefined (reading 'matches')
> 46 |         diagram = new Diagram();
at new ThemeManager (node_modules/gojs/release/go.js:56:149427)
at new Diagram (node_modules/gojs/release/go.js:12:13358)

Anything I need to consider when doing tests in goJS v3?

The ThemeManager is new in v3.0. Its constructor, among other things, does:

if (root.matchMedia) this._preferredColorScheme = root.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';

So for some reason in your environment there is a “matchMedia” function on the root (window?) object, but it’s returning undefined. That seems to me to be a faulty implementation of your test environment. Window: matchMedia() method - Web APIs | MDN
What testing framework are you using?

But I don’t understand why it works for the first construction of a Diagram but not subsequent ones.

Thanks for the explanation.
I can see that we are manually setting the matchMedia property in our jest setup.
So probably that is causing the issue.
I’ll try to fix it and in case there is anything relevant to goJS I’ll report back.

Thanks!

In our global jest setup, we were mocking matchMedia as described here.

In our beforeEach function, right after instantiating the diagram, we were calling jest.resetAllMocks();

Seems like this also affected the global matchMedia mock. Therefore the matchMedia property was still there after the first test, but it was undefined.

So all of your tests are working again?

Yes, thank you. The hint for matchMedia helped a lot.