xaviergonz / mobx-keystone

A MobX powered state management solution based on data trees with first class support for Typescript, support for snapshots, patches and much more
https://mobx-keystone.js.org
MIT License
555 stars 25 forks source link

[mobx-keystone] a model with name "PageModel" already exists (if you are using hot-reloading you may safely ignore this warning) #194

Closed airtonix closed 3 years ago

airtonix commented 3 years ago

I know it was mentioned (vaguely) here that avoiding the "Model already exists" warning in jest involves globalConfig, however being the absolute novice (at least compared to @xaviergonz) that I am, perhaps I am DoingItWrong(tm):

Everyone of our models that ends up involved in a test throws a warning that is some variation of this:

  console.warn
    [mobx-keystone] a model with name "AttachmentFieldModel" already exists (if you are using hot-reloading you may safely ignore this warning)

      24 |  */
      25 | @model(ATTACHMENTFIELD_MODEL_KEY)
    > 26 | export class AttachmentFieldModel extends ExtendedModel(BaseFieldModel, {

My jest setup:

src/
  store/
   **/*
jest.config.js
jest.config.beforeeach.js
jest.config.beforeeach-mobxkeystone.js
jest.config.setup-enzyme.js
jest.config.setup-mobxkeystone.js
jest.config.setup-polyfills.js
// jest.config.js
...

  globals: {
    keystoneCounter: 0
  },

  setupFilesAfterEnv: [
    `<rootDir>/packages/${packageName}/jest.config.beforeeach.ts`,
    `<rootDir>/packages/${packageName}/jest.config.beforeeach-mobxkeystone.js`,
  ],

  setupFiles: [
    `<rootDir>/packages/${packageName}/jest.config.setup-enzyme.js`,
    `<rootDir>/packages/${packageName}/jest.config.setup-polyfills.js`,
    `<rootDir>/packages/${packageName}/jest.config.setup-mobxkeystone.js`,
  ],
...
// jest.config.setup-mobxkeystone.js

import { setGlobalConfig } from 'mobx-keystone'

global.keystoneCounter = 1

setGlobalConfig({
  modelIdGenerator () {
    return `id-${global.keystoneCounter++}`
  },
})
// jest.config.beforeeach-mobxkeystone.js
global.keystoneCounter = 1

If i throw a dirty console log in that modelIdGenerator, then I see a lot of expected spam in the console that the generated id is always 1 since none of our tests cases instantiate more than one instance of a model yet.

If i remove the jest.config.beforeeach-mobxkeystone.js logic, then the model count logged in the console grows as expected, but the error/warning still remains.

Any thoughts/help on properly utilising setGlobalConfig ?

airtonix commented 3 years ago

I will also mention that i've split jest.config.before-mobxkeystone.js and jest.config.setup-mobxkeystone.js in the jest config because you're don't have access to beforeEach within the setupFiles stage.

airtonix commented 3 years ago

I've also had a look at https://github.com/xaviergonz/mobx-keystone/blob/master/packages/lib/test/commonSetup.ts and tried to imitate its use, but we still get the error/warning mentioned above.

xaviergonz commented 3 years ago

Does it happen when jest is running in watch mode, when it runs in single run mode or both?

xaviergonz commented 3 years ago

Btw, the model Id generator is what generates $modelId keys, so it is totally unrelated to the warning you get.

Also, I just added showDuplicateModelNameWarnings to the global config in v0.48.2, which you can set to false to get rid of those warnings in testing envs (such as jest).

airtonix commented 3 years ago

@xaviergonz I'll give that a whirl.

also:

airtonix commented 3 years ago

fixed! legend.