typescript - 如何配置 Jest 以转换包含无效语法的模块?

标签 typescript react-native jestjs babeljs babel-jest

我已经 fork 了以下 react native base项目并将其从 JavaScript 转换为 TypeScript。该应用程序运行正常,但 Jest 测试失败并显示以下错误。我希望测试能够成功运行,并且 Jest 会在必要时运行任何转换:

 C:\Users\brian-varley\Documents\Projects\react-native-base\node_modules\redux-flipper\node_modules\react-native-flipper\index.js:11
import {NativeModules, NativeEventEmitter} from 'react-native';
^^^^^^

SyntaxError: Cannot use import statement outside a module

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
  at Object.<anonymous> (node_modules/redux-flipper/lib/index.js:3:32)
错误的原因似乎来自这个import react-native-flipper里面的声明包裹:
import {NativeModules, NativeEventEmitter} from 'react-native';
我在这里从 StackOverflow 尝试了一个解决方案,它建议使用 babel-jest作为 jest.config.js 文件中的转换器无效 - https://stackoverflow.com/a/64223627/1829251 .我也查看了这个线程,但没有找到解决方案 - https://github.com/facebook/jest/issues/9292
题:
如何配置 Jest 以转换包含无效语法的模块?
环境
"react-native": "0.63.2",
"jest": "^26.6.3",
"metro-react-native-babel-preset": "0.59.0",
"babel-jest": "^25.1.0",
"ts-jest": "^26.5.3",
配置代码示例:
jest.config.js:
module.exports =  {
    preset: "react-native",
    transform: {
      "^.+\\.tsx?$": "ts-jest",
      "^.+\\.(js|jsx)$": "babel-jest",
      "node_modules/variables/.+\\.(j|t)sx?$": "babel-jest"
    },
    testRegex: "(/src/.*\\.(test|spec))\\.(jsx?|tsx?|ts|js)$",
    transformIgnorePatterns: [
      "node_modules/(?!(jest-)?react-native|react-(native|universal|navigation)-(.*)|@react-native-community/(.*)|@react-navigation/(.*)|bs-platform|@rootstrap/redux-tools)"
    ],
    setupFiles: [
      "./tests/__mocks__/index.js",
      "<rootDir>/node_modules/react-native-gesture-handler/jestSetup.js"
    ],
    moduleFileExtensions: [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ]
  }
babel.config.js:
module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    [
      'module-resolver',
      {
        alias: {
          actions: './src/actions',
          httpClient: './src/httpClient',
          services: './src/services',
          components: './src/components',
          constants: './src/constants',
          screens: './src/screens',
          hooks: './src/hooks',
          locale: './src/locale',
          reducers: './src/reducers',
          selectors: './src/selectors',
          store: './src/store',
          utils: './src/utils',
          navigators: './src/navigators',
          validations: './src/validations',
        },
      },
    ],
  ],
};
tsconfig.json:
{
    "compilerOptions": {
      "target": "es6",
      "module": "commonjs",
      "moduleResolution": "node",
      "jsx": "react",
      "outDir": "./dist",
      "experimentalDecorators": true,
      "emitDecoratorMetadata": true,
      "allowSyntheticDefaultImports": true,
      "strict": true,
      "skipLibCheck": true,
      "declaration": true,
      "noUnusedLocals": true,
      "suppressImplicitAnyIndexErrors": true,
      "baseUrl": "./",
    },
    "exclude": [
      "node_modules",
    ]
  }

最佳答案

module.exports =  {
preset: "react-native",
transform: {
    "^.+\\.ts?$": "ts-jest",
    "^.+\\.tsx?$": "ts-jest",
    "^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
  },
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)?$",
  transformIgnorePatterns: [
    "node_modules/(?!(jest-)?react-native|react-(native|universal|navigation)-(.*)|@react-native-community/(.*)|@react-navigation/(.*)|bs-platform|@rootstrap/redux-tools)"
  ],
  setupFiles: [
    "./tests/__mocks__/index.js",
    "<rootDir>/node_modules/react-native-gesture-handler/jestSetup.js"
  ],
  moduleFileExtensions: [
    "ts",
    "tsx",
    "js",
    "jsx",
    "json",
    "node"
  ],
  moduleNameMapper: {
    "^.+\\.(css|less|scss)$": "identity-obj-proxy"
  },
  moduleDirectories: [
    "node_modules",
    "src"
  ],
  }
你能试试这个文件并尝试创建新的测试吗?

关于typescript - 如何配置 Jest 以转换包含无效语法的模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66959585/

相关文章:

typescript - 类型 'connectedCallback' 上不存在属性 'HTMLElement'

javascript - Angular2-TypeScript : Adding an array to FormControl

reactjs - 为什么在react-select中使用自定义样式时出现 "No overload matches this call"TypeScript错误?

javascript - 如何在 Jest 测试中真正调用 fetch

jestjs - 在 GitLab CI 上使用 Playwright 时出现 WebSocket 错误

reactjs - 测试 React 组件时 Jest AutoMocking 是否有效?

reactjs - 如何从 react-i18next 中 't' 函数的参数中提取翻译键到单独的类型?

react-native - 为什么我的 React Native 导航栏的文本不在垂直中心?

node.js - React Native 入门项目捆绑失败,出现 Unexpected Token 错误

reactjs - 类型错误 : Cannot assign to read only property 'x' of object '#<Object>' React/JEST