Skip to content

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

@martaver

Description

@martaver

🐛 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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions