-
Notifications
You must be signed in to change notification settings - Fork 73
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
Pre save hook called twice during jest testing #61
Comments
could you provide an reproduction repository? |
@hasezoey https://github.com/JakeAi/nestjs-typegoose-pre-hook Run the app with Add a mongodb connection string in test.modules.ts In sso.module.spec.ts you will see two lines TypegooseModule.forFeature([User,PasswordToken]), // breaks test, calls user pre hook twice
// TypegooseModule.forFeature([User]), // doesnt break test, DOESNT call pre hook twice |
@JakeAi your repo does not exists ("Page Not Found / 404") |
@hasezoey my bad, my stuff defaults to private. Try it now. |
@JakeAi i ran it, but it dosnt seem to be the tests (it started the app), but as i read, it is about the tests, right? |
Oops, yes. The sso.module.spec.ts is ran with jest. |
@JakeAi any specific command or just |
as general notes: for the connection options you specified already-default values and incompatible options (the options |
I just added a missing jest.config file. I use webstorm to run the tests so I'm not sure what it actually runs. I believe I removed those options in my original project, but when I cloned it I must not've cloned the latest. |
i executed your example with the mentioned edits, and i can not see the hook being executed twice |
Jest sso.module.spec.ts produces the same results as webstorm for me. |
0001-WIP.patch.txt (.txt added, because github dosnt allow .patch) |
This is with your patch
|
@JakeAi now it is appearing too, but when i sent this patch, it was execution just fine |
now i executed it with |
It happens on both my macbook and windows :( |
Is it a race condition or something somewhere? So, I wonder... if in production I may or may not notice it, but since I'm running tests ( I JUST switched to nest this week and started porting my express to nest ) I am noticing it because I'm running the test constantly... |
i will look more into it tomorrow |
Now I'm getting it to happen regardless of those two lines I comment and uncomment. Sounds good, thanks @hasezoey Must be solar flares |
@JakeAi i investigated a bit, and it seems |
But why does the hook call twice? |
This part means it’s called twice. It takes the password, hashes it, then takes the hashed password and hashes it again. Then saves.
|
Sometimes it works but most of the time it’s hashes twice for me. Is there anything else I can do to log on my end? |
as to why it gets called twice? it is added twice - still need to figure out why any where |
i found the reason on why it is added twice: it is once added to the typegoose maps when the file Update: it somehow gets imported twice, which resets the User class (all @prop generated things) but because the hooks are not in the class, it dosnt get reset |
@hasezoey My buddy said
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
moduleNameMapper: {
"^@mm-mono(.*)": "<rootDir>/libs$1"
}
}; |
after adding |
so i would consider this issue as "not an problem with nestjs-typegoose and typegoose" (except maybe the the mention issue that hooks are still specially handled) |
The moduleNameMapper seems to be the ticket from what i can tell. I'm not concerned about the order of tests are ran in, that's a different animal. The race condition I was talking about (why it was being inconsistent) was just MAYBE something deep within nestjs-typegoose, but apparently not. |
yes, but i meant when you do something like execute the suite case |
Oh, yeah. That's fine. I had JUST started porting my express code when this issue came up. I also was coming from a testless environment. |
@JakeAi so is this issue finished? or should it be kept open? PS: happy porting :) |
I'd say it's finished... because the original jest config came from https://nx.dev/. Unless maybe there is a more recommended jest config approach? |
not that i know of |
@hasezoey I created an issue where it needs to be fixed. And I referenced this issue for information. |
I have been trying to track down this issue all day and cannot figure out what is going on. I have a simple service with a register function. My automated login after registration test keeps failing. I found it's because the pre-save hook gets called twice and re-hashes the hashed password. This behavior does not happen during normal operation... only under jest testing.
This code produces the console log at the bottom.
The text was updated successfully, but these errors were encountered: