7

I have this setup:

// tsconfig.json

{
  "compilerOptions": {
    "rootDir": "src",
  ...
}

...

//jest.config.ts

...
globals: {
  'ts-jest': {
    tsconfig: {
      rootDir: '.'
    }
  }
}

But when I run jest I get an emit error saying that typescript files need to be under root, it seems like this "rootDir" compiler option is being ignored.

I don't want my /test or my configs to be under /src and I don't want it to be compiled to /lib or to be emitted.

I also can't exclude it using exclude because then it's also excluding the types for my .ts files which are outside the project, which makes using ts-node really difficult.

2 Answers2

5

Defining ts-jest config under jest.globals has been deprecated.

// jest.config.ts
...
transform: { "^.+\\.tsx?$": ["ts-jest", {"rootDir": "."}] },
...
Ian Carter
  • 548
  • 8
  • 7
4

You have to specify the ts-node config in tsconfig.json.

// tconfig.json

{
  "compilerOptions": {
    "rootDir": "src",
...
  "ts-node": {
    "transpileOnly": true,
    "files": true,
    "compilerOptions": {
      "rootDir": "."
    }
  }
}

...

// jest.config.ts

...
globals: {
  'ts-jest': {
    tsconfig: {
      rootDir: '.'
    }
  }
}

You can specify a different rootDir for ts-node in the tsconfig.json file that way, which will make ts-node compile using a different root than normally running tsc.

The emit error you're getting when running ts-jest is actually not ts-jest, it's ts-node complaining about not being able to parse the jest config file before it's even ever compiled. When you run jest with a jest.config.ts file it will use ts-node to compile that file, then it will pass it to ts-jest which will compile your tests, then it will pass those .js tests to jest to run it.

This allows you to have a different rootDir for ts-node so that you can run .ts files without needing to compile them to /lib and without having to store them in /src.