首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >正在尝试将jest config中的节点模块列入白名单

正在尝试将jest config中的节点模块列入白名单
EN

Stack Overflow用户
提问于 2018-08-15 23:44:51
回答 1查看 629关注 0票数 0

我希望这只是我的一个简单的错误,但这是我正在做的。我有一个react native项目,我正在通过url导入另一个react native项目,该项目将成为一个公用库。因为它还处于早期阶段,所以我还没有转换这个项目中的代码。我可以从那里导入组件并运行webapp,但我在测试(jest)方面遇到了问题。我深入研究了一下,发现您应该在jest配置中将未转换的节点模块列入白名单,如下所示:

代码语言:javascript
运行
复制
transformIgnorePatterns: [
    "<rootDir>/node_modules/(?!(react-native|my-module))/"
],

但是,这对我在运行测试时遇到的错误没有任何影响:

代码语言:javascript
运行
复制
FAIL  src/App.test.js

测试套件运行失败

代码语言:javascript
运行
复制
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)

有人能指出我(可能)的基本错误吗?

EN

回答 1

Stack Overflow用户

发布于 2018-08-17 15:41:07

通过对package.json中的babel配置进行一些修改,解决了这个问题:

代码语言:javascript
运行
复制
"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"
+        ]
       }
     }
   },
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51862035

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档