Comments (35)
could you provide an reproduction repository?
from nestjs-typegoose.
@hasezoey https://github.com/JakeAi/nestjs-typegoose-pre-hook
Run the app with ng serve mm-n-api
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
from nestjs-typegoose.
@JakeAi your repo does not exists ("Page Not Found / 404")
from nestjs-typegoose.
@hasezoey my bad, my stuff defaults to private. Try it now.
from nestjs-typegoose.
@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?
from nestjs-typegoose.
Oops, yes. The sso.module.spec.ts is ran with jest.
from nestjs-typegoose.
@JakeAi any specific command or just jest sso.module.spec.ts
?
from nestjs-typegoose.
as general notes: for the connection options you specified already-default values and incompatible options (the options autoReconnect
, reconnectTries
and reconnectInterval
are incompatible with useUnifiedTopology
) and i upgraded typegoose, mongoose and types-mongoose just to be sure
from nestjs-typegoose.
@JakeAi any specific command or just
jest sso.module.spec.ts
?
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.
from nestjs-typegoose.
i executed your example with the mentioned edits, and i can not see the hook being executed twice
executed with jest
/ jest --watch
in the repo-root folder
from nestjs-typegoose.
Jest sso.module.spec.ts produces the same results as webstorm for me.
from nestjs-typegoose.
0001-WIP.patch.txt (.txt added, because github dosnt allow .patch)
this patch includes all my changes made, apply it with git am < 0001-WIP.patch.txt
(if on linux / macos)
from nestjs-typegoose.
This is with your patch
console.warn node_modules/mongoose/lib/helpers/printJestWarning.js:4
Mongoose: looks like you're trying to test a Mongoose app with Jest's default jsdom test environment. Please make sure you read Mongoose's docs on configuring Jest to test Node.js apps: http://mongoosejs.com/docs/jest.html
console.log node_modules/@typegoose/typegoose/lib/prop.js:30
Added "User.email" to the Decorator Cache
console.log node_modules/@typegoose/typegoose/lib/prop.js:30
Added "User.password" to the Decorator Cache
console.log node_modules/@typegoose/typegoose/lib/prop.js:30
Added "PasswordToken.user" to the Decorator Cache
console.log node_modules/@typegoose/typegoose/lib/prop.js:30
Added "User.email" to the Decorator Cache
console.log node_modules/@typegoose/typegoose/lib/prop.js:30
Added "User.password" to the Decorator Cache
console.info node_modules/@typegoose/typegoose/lib/internal/utils.js:446
Assigning global Schema Options to "User"
console.log node_modules/@typegoose/typegoose/lib/internal/schema.js:28
_buildSchema Called for User with options: {}
console.log node_modules/@typegoose/typegoose/lib/prop.js:41
Starting to process "User.email"
console.log node_modules/@typegoose/typegoose/lib/prop.js:41
Starting to process "User.password"
console.info node_modules/@typegoose/typegoose/lib/typegoose.js:181
Class "User" already existed in the constructors Map
console.info node_modules/@typegoose/typegoose/lib/internal/utils.js:446
Assigning global Schema Options to "PasswordToken"
console.log node_modules/@typegoose/typegoose/lib/internal/schema.js:28
_buildSchema Called for PasswordToken with options: {}
console.log node_modules/@typegoose/typegoose/lib/prop.js:41
Starting to process "PasswordToken.user"
console.log node_modules/@typegoose/typegoose/lib/internal/schema.js:73
Applying Index: { fields: { createdAt: 1 },
options: { expireAfterSeconds: 7200 } }
console.info node_modules/@typegoose/typegoose/lib/typegoose.js:181
Class "PasswordToken" already existed in the constructors Map
console.log apps/mm-n-api/src/app/modules/sso.module/sso.service.ts:23
before new
console.log apps/mm-n-api/src/app/modules/sso.module/sso.service.ts:25
{ _id: 5e365736d63a3471b1b45709,
email: '[email protected]',
password: 'Test' }
console.log apps/mm-n-api/src/app/modules/sso.module/sso.service.ts:26
new
console.log apps/mm-n-api/src/app/modules/sso.module/sso.service.ts:27
before validate
console.log apps/mm-n-api/src/app/modules/sso.module/sso.service.ts:29
validate
console.log apps/mm-n-api/src/app/modules/sso.module/sso.service.ts:30
before save
console.log libs/api/models/user.model.ts:12
$2a$10$765hBdjD0.rXStUflPh0ZOEHNVmIWKUYxgvpyDibL3NHiTEMctcim $2a$10$765hBdjD0.rXStUflPh0ZOEHNVmIWKUYxgvpyDibL3NHiTEMctcim
console.log libs/api/models/user.model.ts:12
$2a$10$TsEF5GjbxsMZxvheStc5Oua5TELUhDFrwRsbFghPjFGu.tOQWDCRq $2a$10$TsEF5GjbxsMZxvheStc5Oua5TELUhDFrwRsbFghPjFGu.tOQWDCRq
console.log apps/mm-n-api/src/app/modules/sso.module/sso.service.ts:32
save
console.log apps/mm-n-api/src/app/modules/sso.module/sso.module.spec.ts:96
{ statusCode: 401, error: 'Unauthorized' }
Error: expected 200 "OK", got 401 "Unauthorized"
from nestjs-typegoose.
@JakeAi now it is appearing too, but when i sent this patch, it was execution just fine
from nestjs-typegoose.
now i executed it with jest --watch
/ jest --watchAll
and it just runs fine, and when just using jest
(run once) it fails, uh what
from nestjs-typegoose.
It happens on both my macbook and windows :(
from nestjs-typegoose.
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...
from nestjs-typegoose.
i will look more into it tomorrow
from nestjs-typegoose.
Now I'm getting it to happen regardless of those two lines I comment and uncomment. Sounds good, thanks @hasezoey
Must be solar flares
from nestjs-typegoose.
@JakeAi i investigated a bit, and it seems should respond 200 with jwtAccessToken
is dependent on should succeed with HTTP 201 Created
(to have the user created) maybe this is your race condition?
from nestjs-typegoose.
But why does the hook call twice?
from nestjs-typegoose.
This part means it’s called twice. It takes the password, hashes it, then takes the hashed password and hashes it again. Then saves.
console.log apps/mm-n-api/src/app/modules/sso.module/sso.service.ts:30
before save
console.log libs/api/models/user.model.ts:12
$2a$10$765hBdjD0.rXStUflPh0ZOEHNVmIWKUYxgvpyDibL3NHiTEMctcim $2a$10$765hBdjD0.rXStUflPh0ZOEHNVmIWKUYxgvpyDibL3NHiTEMctcim
console.log libs/api/models/user.model.ts:12
$2a$10$TsEF5GjbxsMZxvheStc5Oua5TELUhDFrwRsbFghPjFGu.tOQWDCRq $2a$10$TsEF5GjbxsMZxvheStc5Oua5TELUhDFrwRsbFghPjFGu.tOQWDCRq
console.log apps/mm-n-api/src/app/modules/sso.module/sso.service.ts:32
save
from nestjs-typegoose.
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?
from nestjs-typegoose.
as to why it gets called twice? it is added twice - still need to figure out why any where
from nestjs-typegoose.
i found the reason on why it is added twice: it is once added to the typegoose maps when the file User
is in is imported, and once when it is added in the ref of PasswordToken
- i dont really understand why it gets executed again (all the decorators) and the reason it is happening to the hooks and not the properties is because the properties are already on the class, while the hooks are currently on an special map (typegoose/typegoose#44), but i still dont understand why all the hooks get called again
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
-> and i cant get good stack-traces because it only gives me weird ones (like Runtime._execModule
)
from nestjs-typegoose.
@hasezoey My buddy said
the problem is with jest's ts-node transpiler, it loads user.model.ts twice causing the hook to be added twice
Do you get the hash twice like I posted?
yeah, until i compiled it all to js and ran it as js. it's because of ts-node. or the way jest uses ts-node. because the only reason it would run twice logically, is if the hook was added twice. which means the module got executed twice. since it's transpiled in real time i figured it was jest/ts-node
change jest.config.js to:
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
moduleNameMapper: {
"^@mm-mono(.*)": "<rootDir>/libs$1"
}
};
from nestjs-typegoose.
after adding moduleNameMapper
do? because after adding this, everything runs fine (but still the race condition between tests)
from nestjs-typegoose.
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)
from nestjs-typegoose.
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.
from nestjs-typegoose.
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 Login After Registration
alone, it will still fail because it is missing the setup from should succeed with HTTP 201 Created
- that was just an general note
from nestjs-typegoose.
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.
from nestjs-typegoose.
@JakeAi so is this issue finished? or should it be kept open?
PS: happy porting :)
from nestjs-typegoose.
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?
from nestjs-typegoose.
Unless maybe there is a more recommended jest config approach?
not that i know of
from nestjs-typegoose.
@hasezoey I created an issue where it needs to be fixed. And I referenced this issue for information.
from nestjs-typegoose.
Related Issues (20)
- Controller passes the test whereas the services level test don't
- Models with the same name HOT 1
- some error
- Incompatibility with the typegoose model
- Typegoose library hook errors.....Generic type HOT 2
- InjectModel brake on mongoose 5.12.9 and higher HOT 6
- Dependency Injection Inside a Pre save hook HOT 1
- Support/ update to NestJS v8 and others HOT 17
- Discriminator properties not working, only base class.
- MongooseError: Operation `xxx.find()` buffering timed out after 10000ms
- Mongoose version 6.0.5, without usenewurlparser parameter
- Mongoose 6.x Support Issue - no exported member 'ConnectionOptions' HOT 1
- replace Promises with Observables HOT 1
- 'useNewUrlParser' does not exist in type 'TypegooseConnectionOptions' HOT 4
- Can not install module HOT 5
- Dependency Dashboard
- Package maintenance HOT 5
- Using the same models in InjectModel HOT 1
- InjectConnection not supported?
- Not support new @nestjs/common 10 version HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nestjs-typegoose.