Cannot find name 'describe' when using yarn pnp and ts-jest (jest) in monorepo

492 views Asked by At

I am using jazelle and yarn pnp in monorepo. but I cannot run tests like jz test. it seems that @types/jest not setup properly.

My errors:

 FAIL  __tests__/autocomplete/presto/suggest.test.ts
  ● Test suite failed to run

    __tests__/autocomplete/presto/suggest.test.ts:4:8 - error TS6137: Cannot import type declaration files. Consider importing 'jest' instead of '@types/jest'.

    4 import '@types/jest';
             ~~~~~~~~~~~~~
    __tests__/autocomplete/presto/suggest.test.ts:6:1 - error TS2593: Cannot find name 'describe'. Do you need to install type definitions for a test runner? Try `npm i --save-dev @types/jest` or `npm i --save-dev @types/mocha` and then add 'jest' or 'mocha' to the types field in your tsconfig.

    6 describe('Presto SQL Suggest Tests', () => {
      ~~~~~~~~
    __tests__/autocomplete/presto/suggest.test.ts:7:5 - error TS2593: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i --save-dev @types/jest` or `npm i --save-dev @types/mocha` and then add 'jest' or 'mocha' to the types field in your tsconfig.

    7     test('Empty suggest', () => {
          ~~~~
    __tests__/autocomplete/presto/suggest.test.ts:10:9 - error TS2304: Cannot find name 'expect'.

    10         expect(result.length).toBe(0);
               ~~~~~~

I did search and tried to fix my following files based on the suggestions searched. but unluckily, I still cannot resolve my errors.

Below are my files:

tsconfig.json

{
  "typeAcquisition": {
    "include": [
      "jest"
    ]
  },
  "compilerOptions": {
    "types": [
      "node",
      "@types/jest"
    ],
    "typeRoots": [
      "./types",
      "./node_modules/@types"
    ],
    "outDir": "target",
    "sourceMap": true,
    "inlineSources": true,
    "noImplicitAny": true,
    "strict": true,
    "noEmit": true,
    "target": "es2015",
    "module": "commonjs",
    "allowJs": true,
    "checkJs": true,
    "jsx": "react",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "skipLibCheck": true,
    "baseUrl": ".",
    "resolveJsonModule": true,
    "experimentalDecorators": true
  },
  "include": [
    "src/**/*.*",
    "__tests__"
  ],
  "exclude": [
    "node_modules",
    "**/*.spec.ts",
    "**/*.test.ts"
  ],
}

my jest.config.js

module.exports = {
  rootDir: './',
  testEnvironment: 'node',
  preset: 'ts-jest',
  transform: {
    "^.+\\.(ts|js)$": require.resolve('ts-jest')
  },
  moduleFileExtensions: [
    "ts",
    "tsx",
    "js"
  ],
  roots: [
    "<rootDir>/__tests__"
  ],
}

and my devDependencies:

    "@types/jest": "28.1.3",
    "@types/mocha": "^2.2.42",
    "@jest/globals": "25.5.2",
    "antlr4ts-cli": "^0.5.0-alpha.4",
    "babel-eslint": "10.1.0",
    "babel-jest": "28.1.3",
    "babel-plugin-parameter-decorator": "1.0.16",
    "create-universal-package": "^4.3.0",
    "eslint": "7.32.0",
    "eslint-config-fusion": "^6.3.0",
    "eslint-config-prettier": "2.10.0",
    "eslint-plugin-baseui": "10.12.1",
    "eslint-plugin-cup": "2.0.3",
    "eslint-plugin-flowtype": "4.7.0",
    "eslint-plugin-import": "2.22.1",
    "eslint-plugin-jest": "23.20.0",
    "eslint-plugin-jsx-a11y": "^6.2.1",
    "eslint-plugin-prettier": "4.2.1",
    "eslint-plugin-react": "^7.27.0",
    "eslint-plugin-react-hooks": "^4.6.0",
    "flow-bin": "0.131.0",
    "flow-coverage-report": "0.7.0",
    "jest": "28.1.3",
    "jest-environment-jsdom": "28.1.3",
    "jest-environment-jsdom-global": "3.1.2",

any idea? thanks

2

There are 2 answers

0
vlecoq-v On

we had the same problem and it seems that the correct solution according to yarn is to have the devDependencies duplicated in each subRepository (and therefore jest and @types/jest that are creating your errors)

here is the issue

dependecy version can be inforced via constraints

0
Dmytro S On

Try to add explicit imports for each function.

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