How do I use tsconfig references for a project that has separate src and test directories?

Viewed 142

Basically, my project structure looks EXACTLY like this official documentation page: https://www.typescriptlang.org/docs/handbook/project-references.html#an-example-project

/
├── src/
│   ├── converter.ts
│   └── units.ts
├── test/
│   ├── converter-tests.ts
│   └── units-tests.ts
└── tsconfig.json

The docs say:

Previously, this structure was rather awkward to work with if you used a single tsconfig file ... Project references can solve all of these problems and more ... The path property of each [project] reference can point to a directory containing a tsconfig.json file, or to the config file itself (which may have any name)

So it says. But to my frustration, the documentation never shows how to use project references to get this structure working, and that's what my project looks like (with one difference I'll mention later*). I'll explicitly list the requirements of the project, although the documentation already implies this information:

  1. /src should emit to /lib
  2. /test should emit to /libtest
  3. /test ts files can import /src ts files
  4. /src ts files CAN'T import /test ts files
  5. /libtest js files can import /lib js files
  6. /lib js files CAN'T import /libtest js files

Here's the question: How should each tsconfig.json file be written to meet these requirements?


* Here's the one difference that I'd like. This isn't a requirement, simply a nice to have. If your answer doesn't provide a way to do this, that's fine.

The docs show example code like this:

// converter-tests.ts
import * as converter from "../converter";
assert.areEqual(converter.celsiusToFahrenheit(0), 32);

Is it possible to meet all the above requirements but be able to import like this?

// converter-tests.ts
import * as converter from "./converter"; // single dot!!!
assert.areEqual(converter.celsiusToFahrenheit(0), 32);

In other words, src and test are separate directories on the file system, but from tsc and libtest's perspective, they're the same directory. FWIW, this is the default convention for the Java maven build tool.

For that to work, when test ts files import from ./..., its emitted js will import from ../libtest/... I think tsconfig paths might have some role for that, but when considered with the other requirements, it breaks my brain to see how it would be used to pull this off.

0 Answers
Related