0

I have some setup problems that result in the TypeScript error below.

Cannot find name 'describe'. Do you need to install type definitions for a test runner?

I installed yarn packages in docker: enter image description here

I have tried the following suggested solutions from Cannot find name 'describe'. Do you need to install type definitions for a test runner? but none of them worked at my end.

  1. Commenting out prop types in tsconfig.json, and created a tsconfig.spec.json - @Melvin Sy
  2. Did the checklist of @Greg Wozniak
  3. Added @types/jest in prop types in tsconfig.json - @Stevo
  4. Creating tests folder - @Jels Boulangier
  5. etc.

Are there any other solutions?

Structure:

- src
    - tests
      -- sample.test.ts
- babel.config.js
- jest.config.ts
- tsconfig.json
- package.json

sample.test.ts

describe('utils', () => { // <----------------------typescript error here and below
  describe('utils#getMessage()', () => {
    it.todo('should return a message');
  });
});

jest.config.ts

export default {
  clearMocks: true,
  verbose: true,
  preset: 'ts-jest',
  collectCoverage: true,
  collectCoverageFrom: ['src/tests/*.ts', '**/*.ts', '!**/*.d.ts'],
  testEnvironment: 'node',
  testRegex: '(src/tests/.*|(\\.|/)(test|spec))\\.[jt]sx?$',
  moduleFileExtensions: ['ts', 'js', 'json', 'node'],
};

tsconfig.json

{
  "compilerOptions": {
    "module": "esnext",
    "target": "es2017",
    "strict": true,
    "sourceMap": true,
    "declaration": true,
    "types": ["@types/jest", "jest", "node"],
    "resolveJsonModule": true,
    "outDir": "./dist",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "moduleResolution": "node",
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": ["src", "tests", "src/tests"]
}

tsconfig.spec.json

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

babel.config.js

module.exports = {
  presets: [
    '@babel/preset-env',
    '@babel/preset-react',
    '@babel/preset-typescript',
  ],
};

package.json

{
  "dependencies": {
    "@types/jest": "^28.1.3",
    "jest": "26.6.0",
    "ts-jest": "^28.0.5",
    ... more code here
  },
  ... more code here
}
  • The first one applies to an Angular project, it doesn't seem like you're actually _using_ `tsconfig.spec.json` anywhere. It's unclear what you have installed without a package file - please give a [mre]. – jonrsharpe Jun 23 '22 at 06:41
  • i have `package.json` sorry. I'll update. – アリ・ナディム Jun 23 '22 at 06:43
  • @Johnny have installed the package(s) correctly? Try to import the package `@types/jest` somewhere in your component, if it does not give suggestion, well it could be possibly mean there was wrong in packages installed. – tempra Jun 23 '22 at 06:45
  • @tempra I put a screenshot photo in the description. As you can see, package were already installed. But I wonder why my `node_modules` is empty ️️. i'll try to run `yarn install` instead of running it inside the docker. – アリ・ナディム Jun 23 '22 at 06:57
  • @tempra oh. the typescript error is gone. It's working now. I think this was all because of the `node_modules`, thank you very much. – アリ・ナディム Jun 23 '22 at 07:00
  • @Johnny nice. good to know it helped. let me post an answer since it helped solved the issue. – tempra Jun 23 '22 at 07:07

2 Answers2

0

have you correctly installed the package(s)?Try to import the package @types/jest somewhere in your component. If it does not give any suggestions, well, it could possibly mean there was something wrong with the packages installed.

It looks like it wasn't able to find the needed package for the method describe. The typescript error was shown because the necessary package was not yet added or installed. Or a failure to import the package.

In your case, you stated that the node_modules are empty despite having already run yarn install within your Docker service. I can't tell you more about Docker since I'm not quite familiar with it. node_modules, on the other hand, should not be empty (in this case), because this is where npm or yarn keeps track of the packages you installed.

tempra
  • 2,455
  • 1
  • 12
  • 27
0

Just add:

import '@types/jest';
Victor Jonah
  • 11
  • 1
  • 1