cannot find name it in jest typescript

后端 未结 4 1258
Happy的楠姐
Happy的楠姐 2020-12-30 02:40

I try to create an intial setup for jest in react + typescript.I have completed the initial setup and try to check whether the test runs. When i run the test using the comma

相关标签:
4条回答
  • 2020-12-30 03:12

    For me, the problem was that my tsconfig.json was built with

      "include": [
        "src"
      ]
    

    I had to change that to

      "include": [
        "src",
        "tests"
      ]
    

    (My tests all being in directory 'tests')

    0 讨论(0)
  • 2020-12-30 03:15

    Install

    npm install jest @types/jest ts-jest
    

    jest.config.js -- at root

     module.exports = {
      roots: ['<rootDir>/src'],
      transform: {
        '^.+\\.tsx?$': 'ts-jest',
      },
      testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
      moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
    }
    

    tsconfig.json

    {
        "compilerOptions": {
         ...
    
          "types": ["reflect-metadata", "jest"],
          "typeRoots": ["./types", "./node_modules/@types"]
    
         ...
        },
        "exclude": ["node_modules", "**/*.spec.ts", "**/*.test.ts"],
        "include": ["./src/**/*.tsx", "./src/**/*.ts"]
      }
    
    0 讨论(0)
  • 2020-12-30 03:18

    In tsconfig.json add the below code

    "types": ["jest"],
    "typeRoots": ["./src/types", "node_modules/@types"],
    
    0 讨论(0)
  • 2020-12-30 03:21

    Updating ts-loader to v6.0.1 or above can do a trick.

    0 讨论(0)
提交回复
热议问题