Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest tests start breaking after upgrading to @typegoose/typegoose v6 #39

Open
duro opened this issue Oct 25, 2019 · 2 comments
Open

Jest tests start breaking after upgrading to @typegoose/typegoose v6 #39

duro opened this issue Oct 25, 2019 · 2 comments

Comments

@duro
Copy link
Contributor

duro commented Oct 25, 2019

I just spent the last day and a half tearing my hair out trying to figure out why my tests were breaking after upgrading to @typegoose/typegoose v6.

Turns out that the typegoose library stores an internal map of all the models that it has bootstrapped, and when you run getModelForClass if the model already exists in that map, it just returns the first created model.

The problem arises when you use something like MongoMemoryServer to spin up and teardown an in memory Mongo server, as well as a new instance of your nest app for each test block.

The connections to each server are created and dropped just fine, however, that model map is still stuck in a singleton living in the typegoose package. So when it comes around to try and bootstrap models for the second test block, the models from the old server are still in that map, and silently get returned back when getModelForClass is called.

So my services were getting old models, still pinned to the old connection.

I managed to fix this by adding the following to nestjs-typegoose in the onModuleDestroy function

  async onModuleDestroy() {
    const connection = this.moduleRef.get<any>(this.connectionName);

    if (connection) {
      await connection.close();
      // ==========
      for (const modelName of connection.modelNames()) {
        connection.deleteModel(modelName);
        typegooseData.models.clear();
        typegooseData.constructors.clear();
      }
      // ==========
    }
  }

I have this living in a temporary fork I created here:

https://github.com/duro/nestjs-typegoose/tree/dist

However, this is likely not the final solution. It appears that typegoose is releasing new functions in v6.1 for deleteModel. So once that drops, the above code you likely change to this:

import { deleteModel } from '@typegoose/typegoose'

  async onModuleDestroy() {
    const connection = this.moduleRef.get<any>(this.connectionName);

    if (connection) {
      await connection.close();
      // ==========
      for (const modelName of connection.modelNames()) {
        deleteModel(modelName);
      }
      // ==========
    }
  }

This new function handles removing the model from mongoose, and the Typegoose model data store.

See here:

https://github.com/typegoose/typegoose/blob/master/src/typegoose.ts#L206

Just leaving this here for anyone else who runs across this and hopefully the don't loose a day and a half to this.

Once v6.1 of @typegoose/typegoose releases, I'm happy to come back around with a real PR to try and integrate this.

@hasezoey
Copy link
Contributor

hasezoey commented Nov 3, 2019

sorry that this is so, why not use something like https://jestjs.io/docs/en/setup-teardown#one-time-setup ?

@hasezoey
Copy link
Contributor

6.1 with deleteModel released some time ago

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants