如何在 React 中使用 Root Import 和 React 测试库?
How to use Root Import with React Testing Library in React?
当我在 ".spec.tsx" 文件中导入要测试的组件或页面时,它无法识别 Babel Root Import。
有什么方法可以配置我的 ".spec.tsx" 以将 "~"~" 识别为我的根导入?
项目基地:
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": {
"~/*": ["*"]
}
}
}
我的.eslintrc.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"
}
}
}
}
我解决了问题,我只需要将 Jest 设置添加到我的 package.json
"jest": {
"moduleNameMapper": {
"~(.*)$": "<rootDir>/src/"
}
}
为遇到同样问题的人留在这里。
当我在 ".spec.tsx" 文件中导入要测试的组件或页面时,它无法识别 Babel Root Import。
有什么方法可以配置我的 ".spec.tsx" 以将 "~"~" 识别为我的根导入?
项目基地: 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": {
"~/*": ["*"]
}
}
}
我的.eslintrc.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"
}
}
}
}
我解决了问题,我只需要将 Jest 设置添加到我的 package.json
"jest": {
"moduleNameMapper": {
"~(.*)$": "<rootDir>/src/"
}
}
为遇到同样问题的人留在这里。