首页 > 解决方案 > 试图让 jest 与 typescript 一起工作,得到描述,毕竟,expect 是未定义的

问题描述

尝试使用 typescript 设置 jest,我的 linter 给了我一些带有描述的红色波浪线,毕竟它说它们是未定义的。

我在这里尝试了一些解决方案ReferenceError: describe is not defined in Jest + Typescript但它们没有用。

我的版本

node: 12.16.3
"@types/jest": "^26.0.7"
"jest": "^26.1.0",
"ts-jest": "^26.1.4",
"typescript": "^3.9.7"

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "target": "es6",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true
  },
  "include": [
    "node_modules/@types",
    "__test__/db/**/**.ts"
  ]
}

jest.config.js:

module.exports = {
  moduleFileExtensions: ['ts', 'js', 'jsx', 'tsx'],
  transform: {
    '^.+\\.(ts|tsx)$': 'ts-jest',
  },
  testMatch: ['**/__tests__/**/*.[jt]s?(x)'],
  testEnvironment: 'node'
};

我的根测试文件夹看起来像这样

__test__
  db
    tables
      users.test.ts 

和我的测试文件的片段应该足够了:

import { Pool } from 'pg';

describe('users table', (): void => {
  const conString: string = 'postgres://postgres:postgres@127.0.0.1:5432/testdb';

并且只是装箱我的 .eslintrc.js:

module.exports = {
  extends: 'airbnb',
  parser: '@typescript-eslint/parser',
  env: {
    node: true,
    es6: true
  },
  rules: {
    'implicit-arrow-linebreak': 'off',
    'comma-dangle': 'off',
    indent: 'off',
    'no-trailing-spaces': 'off'
  },
  plugins: [
    '@typescript-eslint'
  ]
};

有什么想法吗?谢谢。

标签: typescriptjestjs

解决方案


将 jest: true 添加到 .eslintrc.js 中的 env 对象解决了它。


推荐阅读