That means the library is being loaded twice, which should be avoided.
First, try changing it to import * as go from "gojs";, as that’s the typical way of importing. Then make sure all places you are importing GoJS are using the same source, i.e. not using the debug version in one place and the release version in another.
Yes it’s only recently that we tightened up the warning.
I think that one file can reference Go one way and other code references it another way, where even if it is the same file, module resolution is not clever enough to know this and loads them twice. However it is happening, it can be bad because if Go gets loaded twice, some initialization state can happen twice, or two clipboards could exist internally where half the code uses one clipboard and the other half the code uses the other clipboard, so its worth figuring out why.
I am supposing that there are not 2 instances of go.js (or even 2 instances of node_modules/gojs/release/) in the project, though maybe Jest is making copies or something behind the scenes. If not, then maybe try having the import statements reference the file exactly (node_modules/gojs/release/go.js) and see if that fixes it.
Alternatively, you could have 1 file import GoJS, and maybe export the go symbol, and then have the 2nd file import go from that first file.
Sorry that I don’t have a very precise answer, just some guesses. Let us know what you find.
imports a class/module which itself imports gojs, and
auto-mocks another class which also imports gojs
As a workaround we are currently using the module factory for creating mocks instead of auto mocking.
Unfortunately the module factory is more complex/verbose for most cases.
If not, then maybe try having the import statements reference the file exactly (node_modules/gojs/release/go.js) and see if that fixes it.
Unfortunately this didn’t change the outcome.
I could image that jest has some special handling for the imported/mocked modules (somehow load them in a separate context), just to make sure that the tests don’t interfere with each other, especially since test can also run in parallel.
Is there some non-official property I could set on goJs so it doesn’t throw an error if it is loaded twice?
Would it help if I’d set up a repository to reproduce the issue?
No, because loading it twice really can cause subtle problems. We got tired of dealing with the support burden. Which does place more of a burden on developers to make sure their imports are all consistent with each other. And which is why we recommend that programmers copy extensions into their own projects. (Which is also suggested because we do change extensions from time-to-time, ignoring the kind of GoJS version number change.)
I have one library with gojs. I’ll call it canvas-lib. I added a constant that it owns and my parent-repo needs it as well.
In canvas-lib:
export const THING = 1234;
In my parent-repo, we are using canvas-lib, as we were before, but now the constant is available.
But, when I try to import that constant, only my jest tests, all of my all of my jest tests that mock something that indirectly imports import { THING } from 'canvas-lib';, is now giving me the warning:
WARNING: a `go` object on the root object is already defined. version: 2.1.56, replaced with version: 2.1.56
Reading through the responses here, I cannot put 2 and 2 together on what to do next. Thank you for any information.