ts-jest: Does not resolve path from tsconfig.js 'paths' option.
- Issue
[describe the issue here]
Seems to not use path mappings from tsconfig.js that work fine with the rest of my Typescript project. Instead, I have to implement the babel module resolver so I have to maintain two mappings which is not fun.
tsconfig.js:
{
"compilerOptions": {
"module": "commonjs",
"target": "es2017",
"jsx": "react",
"moduleResolution": "node",
"allowJs": true,
"importHelpers": true,
"allowSyntheticDefaultImports": true,
"isolatedModules": false,
"noUnusedLocals": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"suppressImplicitAnyIndexErrors": true,
"pretty": true,
"sourceMap": false,
"skipLibCheck": true,
"baseUrl": "src",
"paths": {
"*": "../node_modules_local/*",
"~/*": "./*"
},
"typeRoots": [
"node_modules/@types"
]
}
}
- Expected behavior
[describe the expected behavior here]
- Link to a minimal repo that reproduces this issue
[If you haven’t already, create the smallest possible repo that reproduces this issue by running npm install and npm test. This will speed up any fixes that this issue might need]
- Optional (but highly recommended) - Configure Travis (or your favorite system) with the minimal repo
This allows potential solutions to be tested against the minimal repo. This saves everyone time and avoids a lot of back and forth.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 35
- Comments: 18 (2 by maintainers)
Commits related to this issue
- test: add moduleNameMappers to jest config Mimic the paths set up in tsconfig to allow jest to process imports with named paths. https://github.com/kulshekhar/ts-jest/issues/414#issuecomment-3698762... — committed to eliasnorrby/ecfg by eliasnorrby 3 years ago
- test: add moduleNameMappers to jest config Mimic the paths set up in tsconfig to allow jest to process imports with named paths. https://github.com/kulshekhar/ts-jest/issues/414#issuecomment-3698762... — committed to eliasnorrby/ecfg by eliasnorrby 3 years ago
- chore: add config to use alias on ts test files https://github.com/kulshekhar/ts-jest/issues/414 — committed to mestihudson/microservices-ticketing by mestihudson 3 years ago
- refactor: change relative by alias/absolute paths https://blog.rocketseat.com.br/path-mapping-typescript/ https://github.com/kulshekhar/ts-jest/issues/414 — committed to mestihudson/microservices-ticketing by mestihudson 3 years ago
I faced this as well, however I didn’t think it was an issue. Although it does make sense that the paths in tsconfig are respected automatically from ts-jest.
I used the
moduleNameMapperjest config property and set the same paths. You could try that as a workaround for the time being?Bear in mind the syntax is different for setting the same paths in
moduleNameMappersuch that:"my-path/*": ["src/my-path/*"]would be:
"my-path/(.*)": "<rootDir>/src/my-path/$1"Hope this helps for the time-being.
I am using the
moduleNameMapper, after stugling to make it work, I finally had to pass:{ prefix: '<rootDir>/' } to the second parameter. Here below is my workingjest-e2e.jsfile.For anyone who might run into the same problem, you may want to specify the absolute path to the mapper in jest.config.js
The way I approached this was to just map the values from the
tsconfig.jsoninto thejest.config.jsfile like so:any reason we can’t have ts-jest resolve the tsconfig paths automatically?
@alexanderby you need to add
in jest config.
ts-jest doesn’t pick up or resolve any files. All files are resolved by jest and then passed in to ts-jest for processing. That’s why jest needs to know how to resolve modules - the above code does that
@CanKattwinkel
moduleNameMapperI honestly don’t know why
../..is required but this seemed to workinstall
ts-jestincore’s directory or in one of its ancestor directoryimplement the
core/some-shared-model.tsfilethis should get tests passing
In my case I used
modulePathsinjest.config.jsand it solved the issue.And my tsconfig.json:
So then I can import
import { foo } from 'core/parser';If you don’t want to expend cycles trying to understand the regex patterns required to port your
tsconfig.jsonpathskey intojest.config.js’smoduleNameMapperkey, I found an npm module to do it for you.Assuming you have thee
tsconfig.jsonandjest.config.jsboth defined in the root of your project, from your project root run:Then add the output as the keys of your jest’s
moduleNameMapperfield.https://www.npmjs.com/package/jest-module-name-mapper
Here’s another example for folks specifically trying to get
~/*to work since copy-pasta is helpful:If you had this tsconfig.json
You can use this jest.config.js:
Thanks, it worked and I used aliases in all my tests. One strange thing is that properties’ order matters https://github.com/alexanderby/malevic/commit/5949e1b8413866d2ff3c3f4f3ae6f91440e2de03
Oh the shared model was a refactoring corpse… But yes after I applied your steps I got it working.
Therefore a very big thank you.
@kulshekhar would you mind to take a look at this minimal reproduction repository?
I’ve a shared core (
./core) project that I’m including into my project. It is located next to a nestjs typescript project (./api) that uses Jest for testing. For tsc I’ve added the correct path mapping in my tsconfig file.After I experienced errors with jest not reading this configuration I found this issue. Therefore a moduleNameMapper was added to the
./api/package.json-File. Maybe my mapping is just wrong but I’m still experiencing errors when runningnpm run test:Could not locate module @core/some-model (mapped as /home/.../path-issue/core)(which actually looks good to me - or like the correct path)Please provide a minimal reproducible repo, this isn’t enough for us to go on.