206

When using TypeScript in conjunction with Jest, my specs would fail with error messages like:

test/unit/some.spec.ts:1:1 - error TS2582: Cannot find name 'describe'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha`.
test/unit/some.spec.ts:2:3 - error TS2582: Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha`.
test/unit/some.spec.ts:3:7 - error TS2304: Cannot find name 'expect'.
test/unit/some.spec.ts:7:1 - error TS2582: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha`.

The types are already installed.

I use:

    "@types/jest": "^23.3.12",
    "jest": "^23.6.0",
    "ts-jest": "^23.10.5",
    "typescript": "^3.1.6"

I run tests using jest --forceExit --coverage --verbose

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ronin
  • 7,322
  • 6
  • 36
  • 54

33 Answers33

156

I'm using Visual Studio Code as my IDE and in my Angular project, and I had to comment-out/remove types in file tsconfig.json and add "jest" in types in tsconfig.spec.json.

File tsconfig.json

{
  "compilerOptions": {
    // "types": []
  }
}

File tsconfig.spec.json

{
  "compilerOptions": {
    "types": ["jest", "node"]
  }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Melvin Sy
  • 1,743
  • 2
  • 11
  • 17
  • 55
    I also had to remove "typeRoots" from my tsconfig file, which was apparently overriding "types". – Freewalker Dec 03 '19 at 18:44
  • 9
    @Freekwalker -- deleting `typeRoots` fixed it for me –  Mar 28 '21 at 04:18
  • 3
    Note: you only need to add `"jest"` to `compilerOptions.types` if it's already defined in your tsconfig. If omitted entirely, the default behavior for that setting is to include all type under `node_modules/@jest`. I encountered this problem because my project was misconfigured with `types: []`, but after removing it entirely I got correctly typed Jest stuff. By defining it the way it's shown in this solution, you're losing implicit inclusion of other types under `node_modules/@types` (which isn't bad, just a side effect to be aware of). – Mark Jul 21 '21 at 15:55
  • I helped a coworker with this issue; you *need* `types` under `compilerOptions`. FYI for others who have this problem and happen to miss this detail – Doug Wilhelm Dec 08 '21 at 17:35
  • 1
    [Official Docs for "typeRoots"](https://www.typescriptlang.org/tsconfig#typeRoots) and [Official Docs for "compilerOptions.types"](https://www.typescriptlang.org/tsconfig#types) are worth a read. – James Dec 09 '22 at 18:30
  • @Freewalker your suggestion worked fine for me – Kimi Raikkonen 9790 Dec 14 '22 at 14:31
  • FWIW, I'm using VSCode 1.74.2, not using Angular, and have no "types" or "typeRoots" settings. Adding the `tsconfig.spec.json` file seems to have no effect, but if I modify the `tsconfig.json` itself to not exclude my tests directory, as described in the answer by Greg Wozniak, then I can make VSCode happy. – Joshua Richardson Jan 23 '23 at 23:16
154

It's a bit tricky one because both: your IDE (i.e., Visual Studio Code) and TypeScript use tsconfig.json for their own purposes.

Simple checklist to solve the initial problem:

(for TypeScript and Jest)

  1. Make sure you have @types/jest and @types/node installed.
  2. Make sure you have linked these types in tsconfig.json so that: "types": ["jest", "node"]
  3. Make sure you don't have your tests or the tests directory excluded from tsconfig.json configuration in excluded property.

Side effect on transpilation

If you transpile from TypeScript to JavaScript using tsc or any custom module that relies on tsconfig.json then you may notice that your tests will be transpiled too in such case (you'll see their .js correspondence in your build directory).

However, in most cases you will have either:

  1. separate tsconfig.prod.json with a configuration that overwrites the default one. There are many settings like inlineSource, sourceMaps, inlineSourceMaps which you'd probably want to disable too and then use tsc --project tsconfig.prod.json to build
  2. terminal (npm/yarn script) command that overwrites the default configuration with the specific flags. Example: npx tsc --inlineSourceMap false --declarationMap false --inlineSources false --sourceMap false. At that point you can use --excludeFiles or --excludeDirectories flag to exclude your tests from the build as per documentation.

The alternative is to use package like rimraf and delete unnecessary files as a part of the build process. It might be less complex than overwriting the configuration and easier to maintain as a build step. In such a case, you may use the command: yarn rimraf build/**/*.test.js.

IDE or TypeScript service restart

Note that sometimes it may take a moment for your IDE (or exactly TypeScript service) to pick up the changes. Give it a few seconds to sink in. At the right bottom frame of your IDE (VSCode or WebStorm) you can find an annotation e.g. "TypeScript 4.2.1". Click on this and select: "Restart service" to be sure that the solution is working on not before you move on.

Greg Wozniak
  • 5,209
  • 3
  • 26
  • 29
  • 7
    What I found is that if I remove , "**/*.spec.ts" from the "excluded" property in tsconfig.json, then the red underline in VS Code goes away for "describe" in .spec files, however those files are now output into /lib folder as part of the package. It seems like there is no way to exclude .spec files from the package, but include partially to get 'describe` errors to go away? – Web Dev Apr 15 '20 at 23:40
  • @Ross just amended my post – Greg Wozniak Apr 20 '20 at 16:04
  • 4
    For me, I also needed to add the "test/" directory to the "include"-section of my `tsconfig.json` and then it worked. This answer gave me the idea that this might be something that needs to be done. – Kira Resari Oct 21 '20 at 06:21
  • 8
    I think this is the most complete answer, However, it's very important to restart VSCode. In my case, I didn't restart which wasted me tons of time. – Jianwu Chen Nov 26 '20 at 05:20
  • I guess the short answer would be add `"jest"` to `types` in `tsconfig.json` so it be come: `"types": ["jest", "node"]`. – H Aßdøµ Dec 20 '21 at 00:16
  • Number 3 was very useful to me. Also #1: @types/jest for `jest`, `describe`, and `it`; @types/node for `global`. It also seems I could have removed `compilerOptions.types` or state them explicitly (thx @Mark). – Cody Jun 15 '22 at 21:50
  • @JianwuChen all the great solutions aside, after applying them, restart VSCode is the 'hidden' gotcha that really needs to be emphasized upon. Thank you. – limco Mar 16 '23 at 02:19
  • @JianwuChen- thank you, I've emphasised this in the answer now. – Greg Wozniak Mar 16 '23 at 09:39
76

This worked for me:

import '@types/jest';
  • 2
    Nice quick fix! – alex1997 Sep 09 '21 at 08:23
  • 14
    It gives me ``Cannot import type declaration files. Consider importing 'jest' instead of '@types/jest'.`` when running tests. – mszan Apr 12 '22 at 15:54
  • 1
    It doesn't have an error but when trying to run the test: `Cannot find module '@types/jest' from 'pages/index.test.tsx'` – KYin Oct 09 '22 at 04:09
  • Can you add more information? E.g., what versions of the different parts? On what platform (Node.js version, npm version, hardware, and operating system, all including editions and versions). – Peter Mortensen Oct 26 '22 at 20:00
  • note that you need to add once, probably only in jest.config.js – cagcak Mar 09 '23 at 07:35
56

None of the previous answers fixed my issue.

I had to add "@types/jest" to the types array in the tsconfig.json file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Stevo
  • 2,601
  • 3
  • 24
  • 32
  • 1
    Thanks, it works for my project which is using typescript and typescript-eslint – swen Oct 21 '20 at 15:07
  • Thank you! btw, in my case it's a mono repo with multiple packages in it and this answer was the only one that fixed the problem. I added it on the root tsconfig.json – awdk Jul 23 '21 at 02:20
  • The only thing that worked for me! Thank you! – Liran H Aug 30 '22 at 12:05
36

The only way I was able to fix this was by adding the tests/ folder to "include" in the tsconfig.json file:

"include": [
  "src/**/*.ts",
  "tests/*.ts"
] 

For those who also have ESLint complaining, you need to add Jest to your .eslintrc.json file:

"env": {
  "es2020": true,
  "node": true,
  "jest": true
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jels Boulangier
  • 734
  • 6
  • 9
  • Thank you, adding to include was the only thing that helped here too! – chitzui May 26 '21 at 11:17
  • Oops, this did work to resolve the error within VS Code, but then my package stopped working (include failing) in other contexts. No idea why that happened, but I'm going to look for another solution. – Joshua Richardson Dec 07 '22 at 03:04
34

After fiddling with the tsconfig.json for a while I finally figured that commenting the "types": [], will work.

Failing configuration (before)

// tsconfig.json
{
  "compilerOptions": {
    "types": []
  }
}

Working configuration

// tsconfig.json
{
  "compilerOptions": {
    // "types": []
  }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ronin
  • 7,322
  • 6
  • 36
  • 54
23

You have to import Jest in your test file:

import 'jest';

Another way to solve it is adding it in your tsconfig.json file:

   "compilerOptions": {
     "types": [ "node", "jest" ],
     "moduleResolution": "node"
   }

If you are using TSLint, the problem may be a not necessary comma at the end of your tsconfig.json file, for example:

{
  "compileOnSave": true,
  "include": [
    "src"
  ],  // Remove this comma
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daniel Manfred
  • 335
  • 2
  • 5
19

In my case (Visual Studio Code, Create React App, Yarn workspaces, Jest v26, @types/jest, "types": ["node", "jest"] present in tsconfig.json) tests were running OK, but the IDE was underlining all describes and its with red.

Nothing helped until I reloaded the Visual Studio Code window after trying quite long.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Distagon
  • 1,018
  • 11
  • 14
  • 2
    Yes, i closed and reopened VS Code completely, then it worked =) Thanks – para Jan 28 '21 at 11:15
  • 1
    Sometimes you can just open up the command palette and choose Restart Typescript Language Server, but of course when I go to look right now I'm not seeing it Maybe the update I just did removed it In my case I forgot to install the npm dependencies on this new project `npm i` and also needed our `.npmrc`. – CTS_AE Jun 02 '22 at 21:16
  • What do you mean by reloading the Visual Studio Code window? Restarting the application? Reloading some window inside of it? Or something else? – Peter Mortensen Oct 26 '22 at 19:24
  • Sometimes, even after closing and opening vscode, it still happens. What fixed it is closing the test file and then opening it again. This is very silly behavior in vs code. – Henry Obiaraije Jan 13 '23 at 20:22
18

The below configuration works for me. I added node_modules/@types to typeRoots.

{
  "compilerOptions": {
    // ...rest of my settings
    "typeRoots": ["node_modules/@types"],
    "types": ["jest", "node"]
  }
}
  • 1
    That worked for me, I've added node_modules/@types to the typeRoots and removed types field from tsconfig – Vlad Rose Dec 03 '21 at 07:30
  • I love you, thanks – Miha Šušteršič Mar 14 '22 at 10:07
  • An explanation would be in order. Why does it work? What is the idea/gist? What is the crucial part? What versions of the different parts were installed? From [the Help Center](https://stackoverflow.com/help/promotion): *"...always explain why the solution you're presenting is appropriate and how it works"*. Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/65665656/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Oct 26 '22 at 19:25
13

You need to include your test path in file tsconfig.json.

Example: suppose you named your path to tests/ and put it in root project directory, you need to specify in "include" parameter from tsconfig to look out for testes files:

  1. Go to: tsconfig.json

  2. Add:

    "include": [
        "tests/*.<file_test_extension>",
    ],
    

    <file_test_extension>: ts | js | etc.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Victor Jatobá
  • 811
  • 7
  • 13
10

You can have a separate tsconfig.json in the test folder __tests__:

{
    "extends": "../tsconfig.json",
    "compilerOptions": {
        "baseUrl": "./",
        "outDir": "../build",
        "noEmit": true,
        "rootDir": "../",
    },
    "exclude": ["node_modules"],
}

which extends the one in the root folder:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "./lib",
    "rootDir": "./src",
    "strict": true,
    "noImplicitAny": true,
    "esModuleInterop": true,
  },
  "exclude": ["node_modules", "**/*.test.ts", "__tests__"]
}

This way, your test files still get excluded from the public build, but still get to share all the common options.

If you are using includes instead of or in conjunction with excludes, make sure to use that in your extension as well, for example:

tsconfig.json

{
  "includes": ["src"],
  ...
}

tests/tsconfig.json

{
  "extends": "../tsconfig.json"
  "includes": ["../"]
}

This won't change what gets included in your build folder, but it will allow Visual Studio Code to find your Jest types.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Polor Beer
  • 1,814
  • 1
  • 19
  • 18
  • this is the correct way.. i excluded `"**/*.test.ts"` because, if i didnt excluded that, VSCode will complaint all `*.ts` should be under `rootDir`.. but if i excluded it, then the error `describe blah...` will arise.. – slier Apr 06 '22 at 06:18
9

You need to include in tsconfig.json your test path.

I solved the problem by having a tsconfig.json and a tsconfig.build.json in my project root. tsconfig.json contains all options including

"include": ["src/**/*", "test/**/*"],

tsconfig.build.json:

{
  "extends": "./tsconfig.json",
  "include": ["src/**/*"]
}

Then in package.json (clean script optional):

"scripts": {
  "clean": "rm -rf dist",
  "build": "npm run clean && tsc --build tsconfig.build.json,
  ...
}
gvlax
  • 740
  • 4
  • 17
Hoopra
  • 201
  • 2
  • 7
  • This was the fix for me. I had the tests excluded in my tsconfig.json file because I didn't want them compiling to the dist folder. I ended up creating a separate tsconfig.build.json with the exclude in there, but left it included in the tsconfig.json and now the warnings are gone, and the tests excluded from the build. Thanks! – amrinea Nov 11 '21 at 16:46
  • This is a response to [Victor Jatobá's answer](https://stackoverflow.com/questions/54139158/cannot-find-name-describe-do-you-need-to-install-type-definitions-for-a-test/59073068#59073068). – Peter Mortensen Oct 26 '22 at 19:12
  • I think this is the most elegant way to solve the issue - just two sibling config files. In my case (I don't have a `test/` folder) in `tsconfig.json` just: `"include": [ "src/**/*.ts"],`, in the `tsconfig.build.json` : `"exclude": [ "**/*.test.ts" ]` – gvlax Mar 02 '23 at 15:00
7

Greg Woz's is the most complete answer. For my case, for some reason, the initial tsconfig.json file contains "exclude": ["node_modules", "**/__tests__/*"], which is the root cause. After that I removed "**/__tests__/*". And make sure it also includes: "types": ["jest"]. It works.

Also, it is important to restart Visual Studio Code after configuration changes. It wastes me hours of time trying all the different ways without restarting it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jianwu Chen
  • 5,336
  • 3
  • 30
  • 35
  • 1
    oh my gah thank you for mentioning that YOU NEED TO RESTART VSCODE! That was my issue this whole time. I spent an hour trying to change my config to no avail, not knowing that a vscode restart was necessary. – Steph M Dec 02 '20 at 18:07
  • 1
    thx man. :) .... f**** restart cost me make all steps from answer above :P – Robert Jan 02 '21 at 17:18
7

Another thing that could be wrong is if you have opened Visual Studio Code in a parent directory above your project. This happened to me because we are using Visual Studio solutions, and I had the entire solution open, not just the project.

Simply put, make sure Visual Studio Code is open to the root of your project.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
6

For Lerna monolithic repository users

I'm running a Lerna monolithic repository and here's what I had to do to fix it:

  1. Ensure "@types/jest" is in the devDependencies of file package.json of both the root package as well as the individual package in the packages/ directory, and you have run lerna bootstrap to install / link those packages in your node_modules directories

  2. Ensure the "types": ["node", "jest"] piece is in your root tsconfig.json.

  3. I added import 'jest'; to the top of my individual *.test.ts files.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aaron_H
  • 1,623
  • 1
  • 12
  • 26
6

The solution suggested by Freewalker in comments can easily be missed. Removing "typeRoots" from the tsconfig file, which was apparently overriding "types" - resolved the issue.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dev
  • 1,451
  • 20
  • 30
6

Since at least Jest 25, it has been possible to import the Jest globals directly. This is usually used in conjunction with the injectGlobals: false config option or with --injectGlobals=false from the CLI.

For example:

import { describe, expect, it, test } from '@jest/globals';

wprl
  • 24,489
  • 11
  • 55
  • 70
  • Can you [elaborate](https://stackoverflow.com/posts/73126240/edit)? From [the Help Center](https://stackoverflow.com/help/promotion): *"...always explain why the solution you're presenting is appropriate and how it works"* (But ****** ***without*** ****** "Edit:", "Update:", or similar - the answer should appear as if it was written today.) – Peter Mortensen Oct 23 '22 at 19:25
4

In my case, the problem was in one particular file. I hadn't found the issue itself, but it was cured by adding import {} from 'jest' to the file's imports.

No other way from the Jest issue tracker, Stack Overflow or what not did help. It was just some crazy bug fixed by some crazy workaround.

Yes, and I added the latest jest, ts-jest and @types/jest to file package.json of course.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fyodor
  • 247
  • 3
  • 14
  • 1
    If you're going to import something, then is better to import the "describe" and "it" functions or whatever you need from jest – Rafael Rozon Nov 14 '19 at 18:44
  • 5
    I used mocha but had the same problem, VSCode was complaining about `describe` and the other types missing even with `.eslintrc` overriding env with mocha. Adding a file in my test dir only doing `import { describe, before, it } from 'mocha'` solved the error in VSCode. – tkhduracell Feb 08 '20 at 02:16
4

None of the previous solutions above helped me.

I was using:

  • Angular 11
  • Jest
  • Uninstalled anything related to Jasmine-Karma
  • .spec files are in same folder as components (auto-gen from ng g)

Adding exclude to tsconfig.app.json (not tsconfig.json) to ignore all specification files when serving the app worked for me.

tsconfig.app.json

"exclude": [
    "**/*.spec.ts"
]

ng s and npm test now work for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
terahertz
  • 2,915
  • 1
  • 21
  • 33
4

Use:

import {} from 'jasmine';

Add the above line to the code.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kumar
  • 41
  • 1
2

What worked for me:

This is happening in Visual Studio Code. You need to run npm i --save-dev @types/jest, and in your

tsconfig.json

you need to place

"jest" in the types under "compilerOptions"

like

"types": ["gapi", "gapi.auth2", "jest"],

and you are done.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kashif Faraz Shamsi
  • 513
  • 1
  • 7
  • 21
2

There can be multiple reasons:

  1. If @types/jest is not installed, try installing it. In the tsconfig.json file, define the types, for example, "typeRoots": ["node_modules/@types/", "./src/@types/", ".src/**/@types/"]

  2. Visual Studio Code issue: try to open Visual Studio Code in the project directory rather than opening it in its parent directory.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
whitetiger1399
  • 423
  • 3
  • 6
2

I found a similar issue was caused by a version number mismatch between @types/jest and jest

1

I am using Mocha, Chai and chai-http for testing a Node.js Express.js project. I was not using types in compilerOptions earlier, but adding the below setting in file tsconfig.json made it work for me:

{
  "compilerOptions": {
    // ...rest of my settings
    "types": ["mocha", "chai", "chai-http"]
  }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ritesh
  • 4,720
  • 6
  • 27
  • 41
1

If you need to install the ts-jest dependency in your project:

yarn add ts-jest -D

In your jest.config.ts file, you need to set the line containing preset: undefined to preset: 'ts-jest'

// A preset that is used as a base for Jest's configuration
preset: 'ts-jest',
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

The packages might have not installed properly. Do check if the package does really exist inside the node_modules folder. Like to this Stack Overflow question, the TypeScript thingy was throwing the error, because its node_modules directory was empty.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tempra
  • 2,455
  • 1
  • 12
  • 27
1

I was still having the issue with mocha even after installing the type definition files from @types/mocha.

Just ensure that if you have configured typeRoots in your tsconfig.json, then node_modules/@types is equally added.

// tsconfig.json
{
  "compilerOptions": {
    "typeRoots": [
       ...
       "node_modules/@types"
    ]
  }
}
Cels
  • 1,212
  • 18
  • 25
0

I installed ts-jest with npm i -D ts-jest and the error disappeared.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nikolay Podolnyy
  • 931
  • 10
  • 19
0

I was missing tsconfig.json, and all I had to do was run tsc --init and Visual Studio Code did not complain about "describe" any more:

{
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig.json to read more about this file */

    /* Basic Options */
    // "incremental": true,                   /* Enable incremental compilation */
    "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
    // "lib": [],                             /* Specify library files to be included in the compilation. */
    // "allowJs": true,                       /* Allow JavaScript files to be compiled. */
    // "checkJs": true,                       /* Report errors in .js files. */
    // "jsx": "preserve",                     /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
    // "declaration": true,                   /* Generates corresponding '.d.ts' file. */
    // "declarationMap": true,                /* Generates a sourcemap for each corresponding '.d.ts' file. */
    // "sourceMap": true,                     /* Generates corresponding '.map' file. */
    // "outFile": "./",                       /* Concatenate and emit output to single file. */
    // "outDir": "./",                        /* Redirect output structure to the directory. */
    // "rootDir": "./",                       /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    // "composite": true,                     /* Enable project compilation */
    // "tsBuildInfoFile": "./",               /* Specify file to store incremental compilation information */
    // "removeComments": true,                /* Do not emit comments to output. */
    // "noEmit": true,                        /* Do not emit outputs. */
    // "importHelpers": true,                 /* Import emit helpers from 'tslib'. */
    // "downlevelIteration": true,            /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
    // "isolatedModules": true,               /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

    /* Strict Type-Checking Options */
    "strict": true,                           /* Enable all strict type-checking options. */
    // "noImplicitAny": true,                 /* Raise error on expressions and declarations with an implied 'any' type. */
    // "strictNullChecks": true,              /* Enable strict null checks. */
    // "strictFunctionTypes": true,           /* Enable strict checking of function types. */
    // "strictBindCallApply": true,           /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
    // "strictPropertyInitialization": true,  /* Enable strict checking of property initialization in classes. */
    // "noImplicitThis": true,                /* Raise error on 'this' expressions with an implied 'any' type. */
    // "alwaysStrict": true,                  /* Parse in strict mode and emit "use strict" for each source file. */

    /* Additional Checks */
    // "noUnusedLocals": true,                /* Report errors on unused locals. */
    // "noUnusedParameters": true,            /* Report errors on unused parameters. */
    // "noImplicitReturns": true,             /* Report error when not all code paths in function return a value. */
    // "noFallthroughCasesInSwitch": true,    /* Report errors for fallthrough cases in switch statement. */
    // "noUncheckedIndexedAccess": true,      /* Include 'undefined' in index signature results */

    /* Module Resolution Options */
    // "moduleResolution": "node",            /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
    // "baseUrl": "./",                       /* Base directory to resolve non-absolute module names. */
    // "paths": {},                           /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
    // "rootDirs": [],                        /* List of root folders whose combined content represents the structure of the project at runtime. */
    // "typeRoots": [],                       /* List of folders to include type definitions from. */
    // "types": [],                           /* Type declaration files to be included in compilation. */
    // "allowSyntheticDefaultImports": true,  /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
    "esModuleInterop": true,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    // "preserveSymlinks": true,              /* Do not resolve the real path of symlinks. */
    // "allowUmdGlobalAccess": true,          /* Allow accessing UMD globals from modules. */

    /* Source Map Options */
    // "sourceRoot": "",                      /* Specify the location where debugger should locate TypeScript files instead of source locations. */
    // "mapRoot": "",                         /* Specify the location where debugger should locate map files instead of generated locations. */
    // "inlineSourceMap": true,               /* Emit a single file with source maps instead of having a separate file. */
    // "inlineSources": true,                 /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

    /* Experimental Options */
    // "experimentalDecorators": true,        /* Enables experimental support for ES7 decorators. */
    // "emitDecoratorMetadata": true,         /* Enables experimental support for emitting type metadata for decorators. */

    /* Advanced Options */
    "skipLibCheck": true,                     /* Skip type checking of declaration files. */
    "forceConsistentCasingInFileNames": true  /* Disallow inconsistently-cased references to the same file. */
  }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matthias
  • 1,150
  • 20
  • 38
0

I came across this issue today, as I was putting together a POC. I am using Protractor and Jasmine (as opposed to Jest or Mocha). It turns out I had to actually create the tsonfig file via a TypeScript utility/package.

Then, adding "jasmine" and "node" to the types array in tsconfig worked fine.

Here is the link I came across: TypeScript Compiler Configuration

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

In my case, I created babel.config.js, and it caused a problem. Also, we added **/*.js to .gitignore for compile files; our team has different environments.

If we use ts-jest, we don't need babel.config.js.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

Besides other answers, for me, explicit imports helped. Other answers with types didn't help because they were already implemented

import { expect, describe, it, test } from '@jest/globals';
Dmitriy Sakhno
  • 453
  • 3
  • 8
-1

These solve my problem by adding @types/jest to my tesconfig file:

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gabriel soft
  • 432
  • 3
  • 7