Попытка внести модуль узла в белый список в конфигурации jest

Я надеюсь, что это простая ошибка с моей стороны, но вот что я делаю. У меня есть собственный проект React, и я импортирую через URL-адрес другой проект React Native, который станет общей библиотекой. Поскольку это только начало, я еще не перенес код в этот другой проект. Я могу импортировать компоненты оттуда и запустить веб-приложение, но у меня проблемы с тестами (шутка). Я немного покопался и обнаружил, что вы должны занести в белый список непереносимые модули node в своей конфигурации шутки, например:

transformIgnorePatterns: [
    "<rootDir>/node_modules/(?!(react-native|my-module))/"
],

Однако это не повлияло на ошибку, с которой я сталкиваюсь при запуске тестов:

FAIL  src/App.test.js

● Не удалось запустить набор тестов.

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:

/path/to/my/workspace/node_modules/my-module/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { AppRegistry } from "react-native";
                                                                                         ^^^^^^

SyntaxError: Unexpected token import

  1 | import {combineReducers} from "redux";
  2 | import {reducer as formReducer} from "redux-form";
> 3 | import partialForm from "my-module/src/reducers/form";
    | ^
  4 | import progress from "my-module/src/reducers/progress";
  5 | // import theme from "my-module/src/reducers/theme";
  6 | 

  at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
  at Object.<anonymous> (src/reducers/index.js:3:1)

Может ли кто-нибудь указать на мою (вероятно) основную ошибку?


person jaucourt    schedule 15.08.2018    source источник


Ответы (1)


Решили эту проблему с помощью некоторых модификаций конфигурации babel в package.json:

"babel": {
     "presets": [
-      "react-native"
+      "react-native-dotenv"
     ],
     "env": {
-      "test-web": {
+      "test:web": {
+        "presets": [
+          "react-native",
+          "es2015"
+        ],
         "plugins": [
           [
-            "react-native-web"
+            "transform-es2015-modules-commonjs",
+            "transform-strict-mode",
+            "syntax-class-properties",
+            "transform-class-properties",
+            "transform-async-to-generator",
+            "transform-flow-strip-types",
+            "transform-react-jsx",
+            "react-native-web/babel"
           ]
         ]
       },
-      "development-web": {
+      "development:web": {
+        "presets": [
+          "react-native",
+          "es2015"
+        ],
         "plugins": [
-          [
-            "react-native-web"
-          ]
+          "transform-es2015-modules-commonjs",
+          "transform-strict-mode",
+          "syntax-class-properties",
+          "transform-class-properties",
+          "transform-async-to-generator",
+          "transform-flow-strip-types",
+          "transform-react-jsx",
+          "react-native-web/babel"
         ]
       },
-      "production-web": {
+      "production:web": {
+        "presets": [
+          "react-native",
+          "es2015"
+        ],
         "plugins": [
-          [
-            "react-native-web"
-          ]
+          "transform-es2015-modules-commonjs",
+          "transform-strict-mode",
+          "syntax-class-properties",
+          "transform-class-properties",
+          "transform-async-to-generator",
+          "transform-flow-strip-types",
+          "transform-react-jsx",
+          "react-native-web/babel"
         ]
       },
-      "test": {
-        "plugins": []
+      "test:native": {
+        "presets": [
+          "react-native-stage-0"
+        ],
+        "plugins": [
+          "syntax-class-properties"
+        ]
       },
-      "development": {
-        "plugins": []
+      "development:native": {
+        "presets": [
+          "react-native-stage-0"
+        ],
+        "plugins": [
+          "syntax-class-properties"
+        ]
       },
-      "production": {
-        "plugins": []
+      "production:native": {
+        "presets": [
+          "react-native-stage-0"
+        ],
+        "plugins": [
+          "syntax-class-properties"
+        ]
       }
     }
   },
person jaucourt    schedule 17.08.2018