简体   繁体   English

SyntaxError:不能在模块外使用 import 语句,如何使用 react-native-svg 为 Web 运行测试单元?

[英]SyntaxError: Cannot use import statement outside a module, how to run test unit with react-native-svg for the Web?

I have the following error when I run yarn test :运行yarn test时出现以下错误:

lerna ERR! yarn run test:unit stderr:
 FAIL   id-check-front  src/tests/App.test.tsx
  ● 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:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
     • 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:

    /home/dka/workspace/github.com/pass-culture/id-check-front/node_modules/react-native-svg-web/index.js:23
    import * as React from "react";
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | import * as React from 'react'
    > 2 | import Svg, { Path } from 'react-native-svg'
        | ^
      3 | import { IconProps, mapIconPropsToSvgProps } from './types'
      4 | 
      5 | function AlertFilled(props: IconProps) {

      at Runtime.createScriptFromCode (../../../node_modules/jest-runtime/build/index.js:1350:14)
      at Object.<anonymous> (../../../packages/id-check/src/components/icons/AlertFilledIcon.tsx:2:1)

My jest.config.js :我的jest.config.js

  moduleNameMapper: {
    '^react-native$': 'react-native-web',
    '^react-native-modal$': 'modal-enhanced-react-native-web',
+    '^react-native-svg$': 'react-native-svg-web',
  },
  transformIgnorePatterns: [
    'node_modules/(?!(jest-)?react-native' +
    '|@react-navigation' +
    '|@ptomasroos/react-native-multi-slider' +
    '|react-navigation' +
    '|@react-native-community/masked-view' +
    '|@react-native-community' +
    '|react-navigation' +
    '|@react-navigation/.*' +
    '|@unimodules/.*' +
    '|unimodules' +
    '|sentry-expo' +
    '|native-base' +
    '|@sentry/.*' +
    '|native-base-*' +
    '|@react-native-firebase/analytics' +
    '|@react-native-firebase/app' +
    '|@react-native-firebase/remote-config' +
    '|@sentry/react-native' +
    '|react-native-geolocation-service' +
    '|@react-native/polyfills' +
+    '|react-native-svg' +
+    '|react-native-svg-web)'
  ],

I expect when babel to transpile what's in node_modules/react-native-svg-web .我希望当 babel 转换node_modules/react-native-svg-web中的内容时。

Instead, babel does not transpile and I have the casual transpilation error for dependencies that are not transformed: SyntaxError: Cannot use import statement outside a module相反,babel 不会转译,对于未转换的依赖项,我有偶然的转译错误: SyntaxError: Cannot use import statement outside a module

Why is the transformation not working?为什么转换不起作用? How can I test my web app that use react-native-svg-web without issues?如何测试使用react-native-svg-web web 应用程序没有问题?

Your jest.config.js looks good to me.你的jest.config.js对我来说看起来不错。 You may want to check that you are also mapping your .js files to a transformer.您可能需要检查您是否也将.js文件映射到转换器。

For example, if you are using TypeScript, the transform property of your jest.config.js should look like:例如,如果您使用jest.config.js ,则 jest.config.js 的transform属性应如下所示:

transform: {
    '^.+\\.(ts|tsx|js)$': 'ts-jest',
},

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 开玩笑的单元测试 - SyntaxError:不能在模块外使用 import 语句 - Jest unit test - SyntaxError: Cannot use import statement outside a module 当我尝试在我的 React 应用程序中运行 npm 测试时出现错误“SyntaxError: Cannot use import statement outside a module” - The error "SyntaxError: Cannot use import statement outside a module" appears when I try to run npm test in my react application 未捕获的语法错误:无法在 React 模块外使用导入语句 - Uncaught SyntaxError: Cannot use import statement outside a module for React 导入 React = &#39;SyntaxError: 不能在模块外使用导入语句&#39; - Importing React = 'SyntaxError: Cannot use import statement outside a module' React 16: SyntaxError: 不能在模块外使用 import 语句 - React 16: SyntaxError: Cannot use import statement outside a module SyntaxError: 不能在模块外使用 import 语句 - SyntaxError: Cannot use import statement outside a module 如何解决“未捕获的语法错误:无法在模块外使用导入语句”? - How to resolve “Uncaught SyntaxError: Cannot use import statement outside a module”? 不能在模块外使用 import 语句 - React Native - Cannot use import statement outside a module - React Native Jest.js 和 Create-react-app:运行测试时出现“语法错误:无法在模块外使用导入语句” - Jest.js and Create-react-app: I get "SyntaxError: Cannot use import statement outside a module" when running test Jest 无法导入 vue-test-utils 并出现“SyntaxError: Cannot use import statement outside a module”异常 - Jest fails to import vue-test-utils with "SyntaxError: Cannot use import statement outside a module" exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM