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

有没有办法将我的".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();
});
});My 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"
]
}My tsconfig.paths.json:
{
"compilerOptions": {
"baseUrl": "src",
"paths": {
"~/*": ["*"]
}
}
}My .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"
}
}
}
}发布于 2020-06-21 19:00:04
我解决了这个问题,我只需要在我的package.json中添加Jest设置
"jest": {
"moduleNameMapper": {
"~(.*)$": "<rootDir>/src/$1"
}
}对于那些经历同样问题的人来说,它就留在这里。
https://stackoverflow.com/questions/62489723
复制相似问题