21

This question is similar to this question: How to use different tsconfig file for tests? But I am asking for visual studio code.

I have two tsconfig files:

  • tsconfig.json for all application files
  • tsconfig.test.json for all test files

tsconfig.json excludes all *.spec.ts while tsconfig.test.json only includes these files.

How can I make visual studio code to understand that an opened .spec.ts file belongs to tsconfig.test.json?

I dont want to separate all my tests into an own test directory with its own tsconfig.json included but rather keep them next to the application files.

Michael Hilus
  • 1,647
  • 2
  • 21
  • 42
  • 1
    For anyone looking for the opposite layout with tests in a separate folder, please see this useful answer on how to make VSCode work properly: https://stackoverflow.com/a/61153019/3082178. – AKd Nov 19 '20 at 11:14
  • Since TS 3.0 there are "project references" that solve this exact problem. https://www.typescriptlang.org/docs/handbook/project-references.html – polkovnikov.ph Jul 02 '21 at 10:53

1 Answers1

7

VS Code has limitation to a single tsconfig.json for now (see: https://github.com/microsoft/vscode/issues/12463).

Considering the project structure is:

  • src/
  • tsconfig.json
  • tsconfig.test.json

Create ./src/tsconfig.json file with content:

{
  "extends": "../tsconfig.test.json"
}
yavulan
  • 267
  • 4
  • 8
  • 1
    Does this mean files that are not tests within src/ will use the src/tsconfig.json ? – xaunlopez Jul 01 '20 at 01:30
  • 1
    yes, unfortunately, in my experience, it does. This means that VS Code will report that "File is not part of a Typescript project." – Greg Beaver Dec 22 '20 at 03:14
  • This is unhelpful, this imports all your test config into your main config. Which defeats the object of having a test config at all – Liam Aug 09 '22 at 08:34
  • 1
    No, tsconfig.json and src/tsconfig.json are different files – yavulan Aug 10 '22 at 15:54