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

[BUG] pathsToModuleNameMapper prefix wrong path resolve #1968

Closed
MaxmaxmaximusAWS opened this issue Sep 18, 2020 · 3 comments · Fixed by #1969
Closed

[BUG] pathsToModuleNameMapper prefix wrong path resolve #1968

MaxmaxmaximusAWS opened this issue Sep 18, 2020 · 3 comments · Fixed by #1969
Labels
🐛 Bug Confirmed Bug is confirmed

Comments

@MaxmaxmaximusAWS
Copy link

jest.config.js:

const { pathsToModuleNameMapper } = require('ts-jest/utils');
const { compilerOptions } = require('./tsconfig');

pathsToModuleNameMapper(compilerOptions.paths, { prefix: compilerOptions.baseUrl } )

tsconfig.json:

{
  "compilerOptions": {
    "baseUrl": "src",
    "paths": {
      "components/*": ["components/*"]
    }
  },
}

pathsToModuleNameMapper compiles to:

{
  '^components/(.*)$': 'srccomponents/$1'
}

but must be:

{
  '^components/(.*)$': '.src/components/$1'
}

Solution:

you need to normalize the path first:

const path = require('path')

function pathsToModuleNameMapper (childPath, {prefix}){
  // const totalPath = prefix + childPath; its WRONG
  const totalPath = path.join(prefix, childPath); // its GOOD
}
@MaxmaxmaximusAWS MaxmaxmaximusAWS added Bug Report Needs Repo Need a minimium repository to reproduce the problem Needs Triage labels Sep 18, 2020
@ahnpnl
Copy link
Collaborator

ahnpnl commented Sep 18, 2020

PR is welcome :) We plan to have a new release soon so if you can quickly make the Pr, it can be included in the release.

@ahnpnl ahnpnl added 🐛 Bug Confirmed Bug is confirmed and removed Bug Report Needs Repo Need a minimium repository to reproduce the problem Needs Triage labels Sep 18, 2020
@splincode
Copy link

@ahnpnl my tests recently crashed and now I can't fix this
#1982

@its-dibo
Copy link

its-dibo commented Jun 30, 2021

I have this issue for the following tsconfig

"paths": {
      "@pkg/*": ["./packages/*"]
    }

pathsToModuleNameMapper resolves paths wrongly

in jest config:

//jest.config.js

console.log([
   pathsToModuleNameMapper(
    compilerOptions.paths 
  ),


   pathsToModuleNameMapper(compilerOptions.paths, {
    prefix: compilerOptions.baseUrl,
  }),

   pathsToModuleNameMapper(compilerOptions.paths, {
    prefix: "<rootDir>/",
  ])

results in:

[
  {"^@pkg/.*$": "./packages/$1" },
  {"^@pkg/.*$": "././packages/$1" },
  {"^@pkg/.*$": "<rootDir>./packages/$1" },

]

all of them are wrong, but the correct value is

  {"^@pkg/.*$": "<rootDir>/packages/$1*" },

the first one gives a relative value to the file that requested the module, but it must be relative to compilerOptions.baseUrl
the second one is totally wrong
the third one just concatenates <rootDir> with ./packages/$1 instead of resolving them

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

Successfully merging a pull request may close this issue.

4 participants