Cannot find name 'expect'

See original GitHub issue

Issue :

I trying to configure a typescript and react project and now I want to add jest to this project.

I get some error after npm run test

Expected behavior :

normal test for 0 error

Error content:

 TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
    src/components/__test__/Child.test.tsx:11:1 - error TS2304: Cannot find name 'beforeEach'.

    11 beforeEach(() =>
       ~~~~~~~~~~
    src/components/__test__/Child.test.tsx:14:1 - 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`.

    14 it("should render without error", () =>
       ~~
    src/components/__test__/Child.test.tsx:15:3 - error TS2304: Cannot find name 'expect'.

    15   expect(child.length).toBe(1));
         ~~~~~~
    src/components/__test__/Child.test.tsx:16:1 - 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`.

    16 it('should render paragraph for each item that has been passed through props', () => {
       ~~
    src/components/__test__/Child.test.tsx:19:3 - error TS2304: Cannot find name 'expect'.

    19   expect(pNodes.length).toBe(testChildProps.items.length);
         ~~~~~~

Minimal repo :

repo

I can add diagnostics to ignore this error but I think this too tricky I want to fix this error but I don’t know how.

   globals: {
     "ts-jest": {
       "diagnostics": {
         "warnOnly": true
       }
     }
   }

anyone help?

I change the path of typeRoots and it working now

"typeRoots": [
   "../node_modules/@types"
 ]

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:25
  • Comments:7

github_iconTop GitHub Comments

154reactions
futurechallengercommented, Nov 2, 2019

Have you guys ever add a jest type in types section in the tsconfig.json:

{
  "compilerOptions": {
    // ...
    "types": ["node", "jest"],
    // ...
  }
}

First you need to intall jest types by yarn add @types/jest -D.

18reactions
akindocommented, Apr 4, 2022

This worked for me: import { describe, expect, test } from '@jest/globals';

Got the solution from here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TS2304: Cannot find name 'describe' or 'expect' - Stack Overflow
I generated a new project through Angular 7.2.1 when I open app.e2e-spec.ts I see TS2304: Cannot find name 'describe' error.
Read more >
[Sovled] Jest error Cannot find name 'it' in TypeScript
To solve the "Cannot find name 'it'" jest error, make sure to install the typings for jest - npm i -D @types/jest and...
Read more >
[SOLVED] Cannot Find Name 'it' Jest TypeScript Error
To fix the “cannot find name 'it'” Jest error, install the type definitions for Jest with npm i -D @types/jest and add them...
Read more >
Jasmine - Cannot find name describe (or beforeEach, expect ...
I knew it could not be the unit tests themselves; it had to be something low-level. Then it occurred to me that in...
Read more >
cannot find name 'expect'.ts(2304) - You.com | The AI Search ...
TS2304 : Cannot find name 'describe' or 'expect' - AngularFixing ... Solution. Make sure you have this dependency installed and import in any...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found