Sny*_*nyp 17 javascript typescript jestjs react-native ts-jest
我正在尝试使用 typescript、react-native、expo 和 jest 构建一个移动应用程序。当我尝试运行测试时:
\n FAIL src/components/Button/tests/Button.test.tsx\n \xe2\x97\x8f Test suite failed to run\n\n Cannot find module '@babel/runtime/helpers/interopRequireDefault' from 'node_modules/react-native/jest/setup.js'\n\n at Resolver.resolveModule (node_modules/jest-resolve/build/resolver.js:324:11)\n at Object.<anonymous> (node_modules/react-native/jest/setup.js:407:30)\nRun Code Online (Sandbox Code Playgroud)\ntsconfig.json
\n{\n "extends": "expo/tsconfig.base",\n "include": ["./src/**/*.ts", "./src/**/*.tsx"],\n "exclude": [\n "node_modules",\n "**/*.spec.ts",\n "**/*.test.ts",\n "**/*.spec.tsx",\n "**/*.test.tsx",\n "jest.config.ts",\n "babel.config.js",\n "metro.config.js",\n "stryker.conf.json"\n ],\n "compilerOptions": {\n "noEmit": true,\n "pretty": true,\n "strict": true,\n "allowJs": true,\n "sourceMap": true,\n "baseUrl": "./src",\n "module": "es2022",\n "target": "ES2021",\n "declaration": true,\n "skipLibCheck": true,\n "jsx": "react-native",\n "esModuleInterop": true,\n "isolatedModules": true,\n "noImplicitReturns": true,\n "resolveJsonModule": true,\n "types": ["jest", "node"],\n "moduleResolution": "node",\n "allowUnusedLabels": false,\n "allowUnreachableCode": false,\n "noFallthroughCasesInSwitch": true,\n "lib": ["ES2021", "ESNext", "DOM"],\n "allowSyntheticDefaultImports": true,\n "forceConsistentCasingInFileNames": true,\n "typeRoots": ["./types", "./node_modules/@types"]\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n笑话配置.ts
\nimport { defaults } from 'ts-jest/presets';\n\nexport default {\n ...defaults,\n clearMocks: true,\n maxWorkers: '50%',\n collectCoverage: true,\n coverageProvider: 'v8',\n preset: 'react-native',\n testEnvironment: 'node',\n cacheDirectory: '.jest/cache',\n coverageDirectory: '.jest/coverage',\n moduleDirectories: ['<rootDir>/src'],\n testPathIgnorePatterns: ['<rootDir>/node_modules/'],\n coveragePathIgnorePatterns: ['<rootDir>/node_modules/'],\n globals: {\n 'ts-jest': {\n tsconfig: 'tsconfig.json'\n }\n }\n};\nRun Code Online (Sandbox Code Playgroud)\nbabel.config.js
\nmodule.exports = function (api) {\n api.cache(true);\n\n return {\n presets: ['babel-preset-expo', 'module:metro-react-native-babel-preset'],\n plugins: [\n [\n 'module-resolver',\n {\n root: ['./src'],\n extensions: ['.ios.js', '.android.js', '.js', '.ts', '.tsx', '.json']\n }\n ],\n [\n 'module:react-native-dotenv',\n {\n safe: false,\n path: '.env',\n verbose: false,\n moduleName: '@env',\n envName: 'NODE_ENV',\n allowUndefined: false\n }\n ]\n ],\n env: {\n production: {\n plugins: ['react-native-paper/babel', 'transform-remove-console']\n }\n }\n };\n};\nRun Code Online (Sandbox Code Playgroud)\n包.json
\n{\n "name": "radar-solidario",\n "private": true,\n "version": "1.0.0",\n "main": "node_modules/expo/AppEntry.js",\n "scripts": {\n "eject": "expo eject",\n "start": "expo start",\n "test": "jest --no-cache",\n "prepare": "husky install",\n "format": "prettier --write .",\n "check-format": "prettier --check .",\n "check-types": "tsc --pretty --noEmit",\n "check-lint": "eslint src --ext ts --ext tsx --ext js --ext jsx",\n "android": "NODE_ENV=development expo start --android",\n "android:prod": "NODE_ENV=production expo start --android",\n "android:build": "NODE_ENV=production expo build:android -t apk",\n "ios": "NODE_ENV=development expo start --ios",\n "ios:prod": "NODE_ENV=production expo start --ios",\n "ios:build": "NODE_ENV=production expo build:ios -t simulator"\n },\n "dependencies": {\n "@expo/vector-icons": "^12.0.5",\n "@hookform/resolvers": "^2.8.5",\n "@react-native-async-storage/async-storage": "^1.15.14",\n "@react-native-community/datetimepicker": "4.0.0",\n "@react-native-community/netinfo": "7.1.3",\n "@react-native-picker/picker": "^2.2.1",\n "@react-navigation/bottom-tabs": "^6.0.9",\n "@react-navigation/native": "^6.0.6",\n "@react-navigation/stack": "^6.0.11",\n "@reduxjs/toolkit": "^1.7.1",\n "axios": "^0.24.0",\n "expo": "^44.0.3",\n "expo-asset": "~8.4.5",\n "expo-cli": "^5.0.3",\n "expo-community-flipper": "^44.0.0",\n "expo-font": "^10.0.4",\n "expo-splash-screen": "~0.14.1",\n "expo-status-bar": "~1.2.0",\n "i18next": "^21.6.5",\n "jwt-decode": "^3.1.2",\n "lottie-react-native": "^5.0.1",\n "metro-react-native-babel-preset": "^0.66.2",\n "moment": "^2.29.1",\n "react": "17.0.2",\n "react-dom": "17.0.2",\n "react-hook-form": "^7.22.5",\n "react-i18next": "^11.15.3",\n "react-native": "https://github.com/expo/react-native/archive/sdk-44.0.0.tar.gz",\n "react-native-countdown-circle-timer": "^2.5.4",\n "react-native-dotenv": "^3.3.1",\n "react-native-flipper": "^0.127.0",\n "react-native-gesture-handler": "^2.1.0",\n "react-native-maps": "0.29.4",\n "react-native-paper": "^4.11.1",\n "react-native-safe-area-context": "3.3.2",\n "react-native-screens": "~3.10.1",\n "react-native-svg": "^12.1.1",\n "react-native-web": "^0.17.5",\n "react-redux": "^7.2.6",\n "react-thunk": "^1.0.0",\n "redux-persist": "^6.0.0",\n "win-node-env": "^0.6.0",\n "yup": "^0.32.11"\n },\n "devDependencies": {\n "@babel/core": "^7.12.9",\n "@babel/runtime": "^7.16.7",\n "@stryker-mutator/core": "^5.5.1",\n "@stryker-mutator/jest-runner": "^5.5.1",\n "@stryker-mutator/typescript-checker": "^5.5.1",\n "@testing-library/jest-dom": "^5.16.1",\n "@testing-library/react": "^12.1.2",\n "@testing-library/user-event": "^13.5.0",\n "@types/jest": "^27.4.0",\n "@types/node": "^17.0.8",\n "@types/react": "~17.0.21",\n "@types/react-native": "~0.66.10",\n "@types/react-native-dotenv": "^0.2.0",\n "@typescript-eslint/eslint-plugin": "^5.7.0",\n "@typescript-eslint/parser": "^5.7.0",\n "babel-plugin-module-resolver": "^4.1.0",\n "babel-preset-expo": "^9.0.2",\n "eslint": "^8.4.1",\n "eslint-plugin-react": "^7.27.1",\n "husky": "^7.0.0",\n "jest": "^27.4.7",\n "msw": "^0.36.3",\n "prettier": "^2.5.1",\n "ts-jest": "^27.1.2",\n "typescript": "~4.5.4"\n },\n "resolutions": {\n "react-native/@jest/create-cache-key-function": "^27.4.2"\n },\n "husky": {\n "hooks": {\n "pre-commit": "yarn lint",\n "pre-push": ""\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n@babel/runtimenode_modules/jest-haste-map/build/index.js看守人用途expo-jesttsconfig.jsonbabel-jestfor transforminjest.config.tsjest.config.ts文件搞了好久,解决的办法基本上就是使用jest-expopreset并去掉moduleDirectories:
笑话.config.json
{
"clearMocks": true,
"maxWorkers": "50%",
"preset": "jest-expo",
"collectCoverage": true,
"coverageProvider": "v8",
"testEnvironment": "node",
"cacheDirectory": ".jest/cache",
"coverageDirectory": ".jest/coverage",
"moduleFileExtensions": ["ts", "tsx", "js"],
"moduleDirectories": ["node_modules", "src"],
"testPathIgnorePatterns": ["<rootDir>/node_modules/"]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16067 次 |
| 最近记录: |