Home > other >  How to fix "Cannot find 'describe'....." in jest?
How to fix "Cannot find 'describe'....." in jest?

Time:06-23

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
}

CodePudding user response:

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.

  • Related