Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vscode intellisense import not working within jest ts tests [closed]

all throughout my project, VSCode will automatically prompt to import a type (the yellow lightbulb appearing). Except within JEST test files (within my tests directory), I get no lightbulb. I can manually write the import, at which point any further imports within the same file are detected.

does anyone know how to fix this?

regards, Tilli

like image 336
uTILLIty Avatar asked Mar 04 '18 10:03

uTILLIty


2 Answers

I was experiencing the same issue, Matt Bierner's response got me to the correct solution.

The Problem

I had this project layout:

./lib/
./src/
./tests/
./tsconfig.json

In my tsconfig I had:

{
  "compilerOptions": {
    ...
    "outDir": "./lib",
    "typeRoots": ["./node_modules/@types"]
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*.spec.ts"]
}

It was the tests being excluded, i.e. "**/*.spec.ts", which was preventing vscode from suggesting imports inside the tests.

Obviously, when this was removed, when build ran, the tests would end up in my ./lib output dir.

I discovered a better solution to remedy both problems.

The Solution

I split the tsconfig.json as follows:

./lib/
./src/
./src/tsconfig.json
./tests/
./tsconfig.base.json
./tsconfig.json
./package.json

The ./tsconfig.base.json file:

{
  "compilerOptions": {
    ...
    "outDir": "./lib",
    "typeRoots": ["./node_modules/@types"]
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*.spec.ts"]
}

The ./tsconfig.json file:

{
  "extends": "./tsconfig.base.json"
}

The ./src/tsconfig.json file:

{
  "extends": "../tsconfig.base.json",
  "compilerOptions": {
    "outDir": "../lib"
  },
  "exclude": ["node_modules", "lib", "**/*.spec.ts"]
}

The build script was modified so that it would use the ./src/tsconfig.json file, which required the -p ./src/ part to be added, as per the example below.

The ./package.json file:

{
  ...
  "scripts": {
      ...
      "build": "tsc -p ./src/"
      ...
    }
  ...
}

I hope this helps anyone that has the same issue.

like image 91
zeroEvidence Avatar answered Nov 15 '22 09:11

zeroEvidence


For my case adding the following to tsconfig.json fixed the problem.

{
  ...
  "include": [
    "src/**/*"
  ]
}

What is strange is that I didn't have this in my configuration from the beginning of the project, suddenly the import intellisense stopped from working, and had to add the above option to config file. I hope this helps.

like image 26
IdontCareAboutReputationPoints Avatar answered Nov 15 '22 09:11

IdontCareAboutReputationPoints