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 Resolver Plugin Doesn't Return Full Path #2418

Closed
JakeAi opened this issue Feb 2, 2020 · 4 comments
Closed

Jest Resolver Plugin Doesn't Return Full Path #2418

JakeAi opened this issue Feb 2, 2020 · 4 comments
Assignees
Labels
blocked: more info needed outdated scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx type: bug

Comments

@JakeAi
Copy link

JakeAi commented Feb 2, 2020

Please make sure you have read the submission guidelines before posting an issue

https://github.com/nrwl/nx/blob/master/packages/jest/plugins/resolver.ts
Jest resolver plugin doesn't return full path. It needs to resolve the actual fqn of the path instead of a partial. This causes Jest to load the same file twice. Once it is required using a relative path ( "./" ), and once when it's required using an alias. ("@mono-api")

Expected Behavior

Files to not be loaded twice.

Current Behavior

Files are being loaded twice causing problems.

Failure Information (for bugs)

Please help provide information about the failure if this is a bug. If it is not a bug, please remove the rest of this template.

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. Clone https://github.com/JakeAi/nestjs-typegoose-pre-hook
  2. npm i
  3. Add a mongodb connection string in apps/mm-n-api/src/tests/test.modules.ts
  4. cd apps/mm-n-api/src/app/modules/sso.module
  5. jest sso.module.spec.ts
  6. in the log you will see
  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' }

These two lines means the pre save hook is being called twice due to the resolver. When compiling the code to js and running jest it works as intended. The hook takes the password, hashes it, sets this.password to the hash, then it gets called again which hashes the hashed password. Refer to the linked issue for more information.
kpfromer/nestjs-typegoose#61

  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

Context

Please provide any relevant information about your setup:

@nrwl/angular : 8.12.0
@nrwl/cli : 8.12.0
@nrwl/cypress : 8.12.0
@nrwl/eslint-plugin-nx : Not Found
@nrwl/express : 8.12.0
@nrwl/jest : 8.12.0
@nrwl/linter : 8.12.0
@nrwl/nest : 8.12.0
@nrwl/next : Not Found
@nrwl/node : 8.12.0
@nrwl/react : Not Found
@nrwl/schematics : Not Found
@nrwl/tao : 8.12.0
@nrwl/web : Not Found
@nrwl/workspace : 8.12.0
typescript : 3.5.3

Other

kpfromer/nestjs-typegoose#61

@vsavkin vsavkin added scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx type: bug labels Feb 5, 2020
@Cammisuli Cammisuli self-assigned this Feb 18, 2020
@Cammisuli
Copy link
Member

Hi @JakeAi, I cloned your repo and follow the steps you provided and got this as my output:

 console.log sso.service.ts:23
    before new

  console.log sso.service.ts:25
    {
      _id: 5e4c4b5f2c5ca0df7f8667da,
      email: 'testuser@test.com',
      password: 'Test'
    }

  console.log sso.service.ts:26
    new

  console.log sso.service.ts:27
    before validate

  console.log sso.service.ts:29
    validate

  console.log sso.service.ts:30
    before save

  console.log ../../../../../../libs/api/models/user.model.ts:12
    $2a$10$v6zdF4QfI8gVqK5B9yHvk.N5CMCx/Rf5C601PYsw6MSaZVzYNiKJ. $2a$10$v6zdF4QfI8gVqK5B9yHvk.N5CMCx/Rf5C601PYsw6MSaZVzYNiKJ.

  console.log sso.service.ts:32
    save

 PASS  ./sso.module.spec.ts
  SSO Module
    SSO Controller
      Registration
        Register User
          ✓ should succeed with HTTP 201 Created (182ms)
          Login After Registration
            Login Success
              ✓ should respond 200 with jwtAccessToken (71ms)

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        5.231s
Ran all test suites matching /sso.module.spec.ts/i.
  console.log sso.module.spec.ts:93
    { _id: '5e4c4b5f2c5ca0df7f8667da', email: 'testuser@test.com', __v: 0 }

There's two things I notice:

  1. The user.model.ts file is only outputting a console once (which I think is what's expected)
  2. The path to the file is absolute

Is there anything else you can provide? For example, Node version, OS version, etc.

@Cammisuli
Copy link
Member

I think this can be closed. If you can provide more info about this, we can reopen.

@chaoyangnz
Copy link

why closed? this is happening. It took me a lot of time to investigate the weird behaviour. Basically when you mix relative and absolute imports, then the same module will be loaded twice, which means some variables in the module will be read as different values.

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
blocked: more info needed outdated scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx type: bug
Projects
None yet
Development

No branches or pull requests

4 participants