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

Posted

技术标签:

【中文标题】如何配置 Jest 以转换包含无效语法的模块?【英文标题】:How to configure Jest to transform modules containing invalid syntax? 【发布时间】:2021-07-01 16:51:31 【问题描述】:

我已经分叉了以下 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)

错误的原因似乎来自react-native-flipper包内的这个import语句:

import NativeModules, NativeEventEmitter from 'react-native';

我在这里尝试了 *** 的解决方案,该解决方案建议在 jest.config.js 文件中使用 babel-jest作为转换器,但无济于事 - https://***.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",
    ]
  

【问题讨论】:

你已经在 transformIgnorePatterns 中忽略了这个模块。我很确定你不应该尝试加载一个真实的,因为这将要求你在 Node 中模拟原生,即模拟它使用的所有东西,从 react-native 到满足预期的程度。 【参考方案1】:
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"
  ],
  

你能不能试试这个文件并尝试创建新的测试。

【讨论】:

以上是关于如何配置 Jest 以转换包含无效语法的模块?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 vue-test-utils 和 Jest 在 Nuxt 中对使用 vuex-module-decorators 语法定义的 Vuex 模块进行单元测试?

如何制作配置文件以导入相同的模块以进行多个玩笑测试?

Jest 无法从 npm 链接模块转换导入

如何导入配置文件,以便在 Jest 测试下的构建包和模块都可以找到它?

如何配置 Jest 以使用 Expo SDK 32

如何配置 playwright-jest 以排除测试套件(规范)文件以进行运行?