question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

ts-jest doesn't respect outDir and rootDir when using project references

See original GitHub issue

šŸ› Bug Report

In a monorepo setup, that uses ts project references, it is possible to get ts-jest to compile child project refs so that tests in the parent project run in jest. This is evident in the repro repository here: https://github.com/Martaver/ts-jest-issue-805-demo which, when you clone and run yarn test navigates my-appā€™s dependency on my-library successfully and compiles and runs the tests.

In this example, index.ts is in the root directory of each project reference and they use the default main entry in package.json, which is index.js.

Most repositories though, keep code under /src, and one of the features of project references is the ability to map a javascript main entry point in package.json back to the originating typescript entry. It can do this through outDir and rootDir.

However, when moving all code to /src and adding ourDir and rootDir configurations to tsconfig.json and setting the corresponding main in package.json of each project reference, the path resolution fails and the following error is revealed:

Cannot find module '@mfellner/my-library' from '../my-app/src/main.ts'

Unless Iā€™m misconfiguring something, it appears ts-jest isnā€™t honouring tscā€™s project resolution behaviour.

Note that tsc -b works fine (executed in packages/my-app for instance).

To Reproduce

Steps to reproduce the behavior:

  1. Clone https://github.com/Martaver/ts-jest-issue-805-demo/
  2. Switch to branch: repro-ts-jest-outDir-rootDir
  3. Run yarn testā€¦ you will notice that my-app fails because it canā€™t load my-library.

Expected behavior

ts-jest to successfully load the my-library module from the project reference, as it does when index.ts is in the root of each project.

Link to repo (highly encouraged)

https://github.com/Martaver/ts-jest-issue-805-demo/ branches:

  • repro-ts-jest-outDir-rootDir (demonstrates error)
  • master (demonstrates working project ref test, in root of project reference)

Debug log:

N/A

envinfo

System:
    OS: macOS 10.14.6 (18G4032)

Npm packages:
    jest: 26.0.1
    ts-jest: 26.1.0
    typescript: 3.9.3

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:21

github_iconTop GitHub Comments

2reactions
alexbrunocommented, Aug 1, 2022

I know this is an old issue, but if anyone else is running into similar problemsā€¦ I just made this on Jest config and it worked for me:

  globals: {
    'ts-jest': {
      isolatedModules: true,
    },
  }
0reactions
ahnpnlcommented, Jun 2, 2020

i will close this as not ts-jest issue because ts-jest doesnā€™t actually perform module resolution but that is jest does. You should look into TypeScript plugin which can rewrite paths, something like ttypescript does.

Related infor you can find here https://github.com/microsoft/TypeScript/issues/15479#issuecomment-468122916

Read more comments on GitHub >

github_iconTop Results From Across the Web

typescript outDir setting in tsconfig.json not working
I'm calling the script from the root dir of the project. Interestingly, running the same script with an --outDir flag works just fine....
Read more >
TypeStrong/ts-loader - module - Gitter
Hi folks. I am working with a package that merely has an index.d.ts at its root (next to package.json) with "types": "./index.d.ts" in...
Read more >
Typescript does not resolve modules through tsconfig.json's ...
This is my tsconfig.json file. Please note that tsconfig.json file is stored not in the root of the project but in ./app/ui directory...
Read more >
Documentation - Project References - TypeScript
Note that as with tsconfig.json / -p , existing TypeScript project properties will not be respected - all settings should be managed using...
Read more >
TypeScript project references | moon
It supports incremental compilation, so only out-of-date or affected projects are processed. The more TypeScript's cache is warmed, the faster it will be....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found