使用Babel7进行转译时的async-storage SyntaxError意外标识符 [英] async-storage SyntaxError Unexpected identifier while transpiling with Babel7

查看:166
本文介绍了使用Babel7进行转译时的async-storage SyntaxError意外标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用@ react-native-community/async-storage并在我的react-native环境中使用以下npm命令将其转换.

After using @react-native-community/async-storage and transpile it with the following npm command in my react-native environment.

"test": "NODE_ENV=test ./node_modules/.bin/mocha --timeout 5000 --require @babel/register \"./src/shared/__tests__/**/*.spec.js\""

我做了一些研究,没有白费.但是我发现这也发生在杰斯特身上.

I did some research and in no vain. But I found it happens to Jest too.

安装了react-native-async-之后,jest测试失败存储

这是我的babel.config.js

this is my babel.config.js

module.exports = {
  env: {
    production: {
    },
    test: {
      presets: [
        '@babel/preset-env'
      ],
    },
  },
};

我只测试非jsx代码,所以@ babel/preset-env似乎工作正常.

I'm only testing non-jsx code only so @babel/preset-env seems to be working alright.

node_modules/@react-native-community/async-storage/lib/index.js:5
import AsyncStorage from './AsyncStorage';
       ^^^^^^^^^^^^

SyntaxError: Unexpected identifier

推荐答案

似乎没有人喜欢回答开玩笑的新手问题....

It seems like no one likes to answer jest newbie questions....

无论如何,当开始学习开玩笑时,我遇到了一些有趣的错误消息,但并没有反映出实际的错误.开发人员可以考虑一些可能的情况.

anyway, when starting to learn jest, I encountered some funny error messages that doesn't reflect the actual error. There are some possible situations a developer can consider.

  1. 您没有嘲笑您的模块说在node_modules内部的A_module,所以其中一个模块说A_modules内部的B_modules使用react的NativeModules,因此Jest无法执行测试.请查看堆栈跟踪或使用调试器找出要模拟的对象.
  2. 模拟使用NativeModules的模块(类似于第1点,但更直接和简洁)
  3. 在继续操作之前,您需要更全面地了解玩笑.阅读文档很好,但是在您认为合适时可以跳到示例.尤其要查看您真正需要的示例,并阅读相关的示例.很多时候,您的Babel设置或模拟方法都不正确.
  1. You didn't to mock your module say A_module that is inside node_modules so one of the modules say B_modules inside A_modules uses a NativeModules from react so Jest cannot performa a test. Please look at stack trace or use a debugger to find out which one you prefer to mock.
  2. Mock a module that uses NativeModules (similar to point 1, but more direct and concise )
  3. You need to understand jest more thoroughly before you proceed. Reading documentation is good but jump to example when you think fit. especially check out the examples that you really need and read related ones. Many of the time, either your Babel setting or mocking methods is incorrect.
  1. 使用jest.mock代替jest.genMockFromModule.为什么?有一些函数或init函数会使您的测试在此语句时崩溃.因为它调用NativeModules或开玩笑不允许的东西.改用模拟.

此问题的解决方案:请参阅此最新解决方案.

Solutions to this question: please refer to this most updated solution.

谢谢

这篇关于使用Babel7进行转译时的async-storage SyntaxError意外标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆