Code Monkey home page Code Monkey logo

Comments (35)

hasezoey avatar hasezoey commented on June 2, 2024

could you provide an reproduction repository?

from nestjs-typegoose.

JakeAi avatar JakeAi commented on June 2, 2024

@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.

hasezoey avatar hasezoey commented on June 2, 2024

@JakeAi your repo does not exists ("Page Not Found / 404")

from nestjs-typegoose.

JakeAi avatar JakeAi commented on June 2, 2024

@hasezoey my bad, my stuff defaults to private. Try it now.

from nestjs-typegoose.

hasezoey avatar hasezoey commented on June 2, 2024

@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.

JakeAi avatar JakeAi commented on June 2, 2024

Oops, yes. The sso.module.spec.ts is ran with jest.

from nestjs-typegoose.

hasezoey avatar hasezoey commented on June 2, 2024

@JakeAi any specific command or just jest sso.module.spec.ts?

from nestjs-typegoose.

hasezoey avatar hasezoey commented on June 2, 2024

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 avatar JakeAi commented on June 2, 2024

@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.

hasezoey avatar hasezoey commented on June 2, 2024

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.

JakeAi avatar JakeAi commented on June 2, 2024

Jest sso.module.spec.ts produces the same results as webstorm for me.

from nestjs-typegoose.

hasezoey avatar hasezoey commented on June 2, 2024

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.

JakeAi avatar JakeAi commented on June 2, 2024

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.

hasezoey avatar hasezoey commented on June 2, 2024

@JakeAi now it is appearing too, but when i sent this patch, it was execution just fine

from nestjs-typegoose.

hasezoey avatar hasezoey commented on June 2, 2024

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.

JakeAi avatar JakeAi commented on June 2, 2024

It happens on both my macbook and windows :(

from nestjs-typegoose.

JakeAi avatar JakeAi commented on June 2, 2024

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.

hasezoey avatar hasezoey commented on June 2, 2024

i will look more into it tomorrow

from nestjs-typegoose.

JakeAi avatar JakeAi commented on June 2, 2024

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.

hasezoey avatar hasezoey commented on June 2, 2024

@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.

JakeAi avatar JakeAi commented on June 2, 2024

But why does the hook call twice?

from nestjs-typegoose.

JakeAi avatar JakeAi commented on June 2, 2024

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.

JakeAi avatar JakeAi commented on June 2, 2024

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.

hasezoey avatar hasezoey commented on June 2, 2024

as to why it gets called twice? it is added twice - still need to figure out why any where

from nestjs-typegoose.

hasezoey avatar hasezoey commented on June 2, 2024

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.

JakeAi avatar JakeAi commented on June 2, 2024

@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.

hasezoey avatar hasezoey commented on June 2, 2024

after adding moduleNameMapper do? because after adding this, everything runs fine (but still the race condition between tests)

from nestjs-typegoose.

hasezoey avatar hasezoey commented on June 2, 2024

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.

JakeAi avatar JakeAi commented on June 2, 2024

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.

hasezoey avatar hasezoey commented on June 2, 2024

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.

JakeAi avatar JakeAi commented on June 2, 2024

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.

hasezoey avatar hasezoey commented on June 2, 2024

@JakeAi so is this issue finished? or should it be kept open?

PS: happy porting :)

from nestjs-typegoose.

JakeAi avatar JakeAi commented on June 2, 2024

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.

hasezoey avatar hasezoey commented on June 2, 2024

Unless maybe there is a more recommended jest config approach?

not that i know of

from nestjs-typegoose.

JakeAi avatar JakeAi commented on June 2, 2024

@hasezoey I created an issue where it needs to be fixed. And I referenced this issue for information.

from nestjs-typegoose.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.