首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在React中使用根导入和React测试库?

如何在React中使用根导入和React测试库?
EN

Stack Overflow用户
提问于 2020-06-20 18:00:42
回答 1查看 709关注 0票数 0

当我导入要在".spec.tsx"文件中测试的组件或页面时,它不会识别Babel。

有没有办法将我的".spec.tsx"配置为识别~“”作为我的根导入?

项目基础: https://github.com/tavareshenrique/go-barber-web-ts

我的代码:

代码语言:javascript
复制
import React from 'react';
import { render } from '@testing-library/react';

import SignIn from '~/pages/SignIn';

describe('SignIn Page', () => {
  it('should be able to sign in', () => {
    const { debug } = render(<SignIn />);

    debug();
  });
});

My tsconfig.json:

代码语言:javascript
复制
{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react"
  },
  "extends": "./tsconfig.paths.json",
  "include": [
    "src"
  ]
}

My tsconfig.paths.json:

代码语言:javascript
复制
{
  "compilerOptions": {
    "baseUrl": "src",
    "paths": {
      "~/*": ["*"]
    }
  }
}

My .eslintrc.json:

代码语言:javascript
复制
{
  "env": {
    "browser": true,
    "es6": true,
    "jest":true
  },
  "extends": [
    "plugin:react/recommended",
    "airbnb",
    "plugin:@typescript-eslint/recommended",
    "prettier/@typescript-eslint",
    "plugin:prettier/recommended"
  ],
  "globals": {
    "Atomics": "readonly",
    "SharedArrayBuffer": "readonly"
  },
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": 2018,
    "sourceType": "module"
  },
  "plugins": [
    "react",
    "react-hooks",
    "@typescript-eslint",
    "prettier"
  ],
  "rules": {
    "prettier/prettier": "error",
    "react/jsx-one-expression-per-line": "off",
    "react/jsx-props-no-spreading": "off",
    "react/prop-types": "off",
    "react-hooks/rules-of-hooks": "error",
    "react-hooks/exhaustive-deps": "warn",
    "no-unused-expressions": "off",
    "react/jsx-filename-extension": [1, { "extensions": [".tsx"] }],
    "import/prefer-default-export": "off",
    "import/no-duplicates": "off",
    "@typescript-eslint/interface-name-prefix": ["error", { "prefixWithI": "always" }],
    "@typescript-eslint/camelcase": "off",
    "@typescript-eslint/explicit-function-return-type": [
      "error",
      {
        "allowExpressions": true
      }
    ],
    "import/extensions": [
      "error",
      "ignorePackages",
      {
        "ts": "never",
        "tsx": "never"
      }
    ]
  },
  "settings": {
    "import/resolver": {
      "typescript": {},
      "babel-plugin-root-import": {
        "rootPathPrefix": "~",
        "rootPathSuffix": "src"
      }
    }
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-21 19:00:04

我解决了这个问题,我只需要在我的package.json中添加Jest设置

代码语言:javascript
复制
"jest": {
   "moduleNameMapper": {
      "~(.*)$": "<rootDir>/src/$1"
   }
}

对于那些经历同样问题的人来说,它就留在这里。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62489723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档