开玩笑:测试失败,出现“ SyntaxError:无法在模块外部使用导入语句” React native

时间:2020-10-26 09:59:04

标签: typescript firebase react-native jestjs

我正在尝试使用jest@testing-library/react-native进行测试 却失败了:

 ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".     

    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in 
your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    C:\Users\Desktop\projects\LearnToCode\node_modules\@react-native-firebase\admob\lib\index.js:18   
    import {
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

    > 1 | import { AdEventType, FirebaseAdMobTypes, InterstitialAd, TestIds } from '@react-native-firebase/admob';  
        | ^

我的package.json

...

 "jest": {
    "preset": "react-native"
  },

...

tsconfig.json

{
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "isolatedModules": true,
    "jsx": "react-native",
    "lib": ["es6"],
    "moduleResolution": "node",
    "noEmit": true,
    "strict": true,
    "target": "esnext",
    
  },
  "exclude": [
    "node_modules",
    "babel.config.js",
    "metro.config.js",
    "jest.config.js"
  ]
}

我的jest.config.js

module.exports = {
  preset: 'react-native',
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
  transformIgnorePatterns: ['@react-native-firebase/admob'],
 };

我的bable.config.js

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
};

我已经搜索了很多东西,已经尝试了很少的东西,但是没有一个起作用。 拜托,让我知道是否缺少什么内容,我将对其进行编辑。 谢谢!

0 个答案:

没有答案