繁体   English   中英

导入react-native-fetch-blob时出现意外令牌

[英]Unexpected Token when importing react-native-fetch-blob

我们正在尝试使用以下代码导入react-native-fetch-blob软件包:

const RNFetchBlob = require('react-native-fetch-blob');
const firebase = require('firebase');

但是,当我们尝试构建时,我们会收到意外令牌导入的语法错误,如下所示。

C:\\ Users \\ ... \\ node_modules \\ react-native-fetch-blob \\ index.js:5
import {
^^^^^^
SyntaxError:意外的令牌导入
at Object.exports.runInThisContext(vm.js:76:16)
在Module._compile(module.js:513:28)
at Object.Module._extensions..js(module.js:550:10)
在Module.load(module.js:458:32)
在tryModuleLoad(module.js:417:12)
在Function.Module._load(module.js:409:3)
在Module.require(module.js:468:17)
at require(internal / module.js:20:19)
在对象。 (C:\\ Users \\ ... \\ build \\ cloud \\ file.js:159:243)在Module._compile(module.js:541:32)

我们使用的是ES6,我们的.babelrc文件如下所示

"plugins": [
    ["transform-runtime", {
        "polyfill": false,
        "regenerator": true
    }]
],
"presets": ["react-native","es2015",]

这个问题有方法解决吗? 任何帮助将不胜感激!

谢谢!

我能够通过为react-native-fetch-blob(在我的例子中为rn-fetch-blob)创建一个模拟来解决这个问题。 这里建议的模拟对我有用https//github.com/wkh237/react-native-fetch-blob/issues/212#issuecomment-340706825

作为@scerelli我也希望模拟在一个单独的地方发生,所以它可以被所有测试使用。 将以下代码放在/ mocks /react-native-fetch-blob.js中似乎对我有用:

const existsMock = jest.fn(); existsMock.mockReturnValueOnce({then:jest.fn()});

export default {
DocumentDir: () => {},
ImageCache: {
    get: {
        clear: () => {},
    },
},
fs: {
    exists: existsMock,
    dirs: {
        MainBundleDir: () => {},
        CacheDir: () => {},
        DocumentDir: () => {},
    },
    },
};

它是@SoundBlaster解决方案的变体,仅在全局模拟文件中。 有了它,我不需要导入或隐式模拟测试代码中的任何内容。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM