Как использовать Root Import с библиотекой тестирования React в React? - PullRequest
0 голосов
/ 20 июня 2020

Когда я импортирую свои компоненты или страницы для тестирования в свой файл ". Spe c .tsx" , он не распознает Babel Root Import.

enter image description here

Is there any way to configure my ".spec.tsx" to recognize "~" as my Root Import?

The Project Base: https://github.com/tavareshenrique/go-barber-web-ts

Мой код:

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();
  });
});

Мой tsconfig. json:

{
  "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"
  ]
}

Мои tsconfig.paths. json:

{
  "compilerOptions": {
    "baseUrl": "src",
    "paths": {
      "~/*": ["*"]
    }
  }
}

Мой .eslintr c. json:

{
  "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"
      }
    }
  }
}

1 Ответ

2 голосов
/ 21 июня 2020

Я решил проблему, мне просто нужно было добавить настройки Jest в свой пакет. json

"jest": {
   "moduleNameMapper": {
      "~(.*)$": "<rootDir>/src/$1"
   }
}

Он останется здесь для тех, кто go столкнулся с той же проблемой.

...