[英]Unexpected Token when importing react-native-fetch-blob
We are trying to import the react-native-fetch-blob package package using the following code: 我们正在尝试使用以下代码导入react-native-fetch-blob软件包:
const RNFetchBlob = require('react-native-fetch-blob');
const firebase = require('firebase');
However, when we try and build, we get a Syntax Error for Unexpected token import as follows. 但是,当我们尝试构建时,我们会收到意外令牌导入的语法错误,如下所示。
C:\\Users\\ ...\\node_modules\\react-native-fetch-blob\\index.js:5
C:\\ Users \\ ... \\ node_modules \\ react-native-fetch-blob \\ index.js:5
import {import {
^^^^^^^^^^^^
SyntaxError: Unexpected token importSyntaxError:意外的令牌导入
at Object.exports.runInThisContext (vm.js:76:16)at Object.exports.runInThisContext(vm.js:76:16)
at Module._compile (module.js:513:28)在Module._compile(module.js:513:28)
at Object.Module._extensions..js (module.js:550:10)at Object.Module._extensions..js(module.js:550:10)
at Module.load (module.js:458:32)在Module.load(module.js:458:32)
at tryModuleLoad (module.js:417:12)在tryModuleLoad(module.js:417:12)
at Function.Module._load (module.js:409:3)在Function.Module._load(module.js:409:3)
at Module.require (module.js:468:17)在Module.require(module.js:468:17)
at require (internal/module.js:20:19)at require(internal / module.js:20:19)
at Object.在对象。 (C:\\Users\\ ...\\build\\cloud\\file.js:159:243) at Module._compile (module.js:541:32)
(C:\\ Users \\ ... \\ build \\ cloud \\ file.js:159:243)在Module._compile(module.js:541:32)
We are using ES6 and our .babelrc file looks as follows 我们使用的是ES6,我们的.babelrc文件如下所示
"plugins": [
["transform-runtime", {
"polyfill": false,
"regenerator": true
}]
],
"presets": ["react-native","es2015",]
Is there a solution to this? 这个问题有方法解决吗? Any help would be greatly appreciated!
任何帮助将不胜感激!
Thanks! 谢谢!
I was able to work around this problem by creating a mock for react-native-fetch-blob (rn-fetch-blob, in my case). 我能够通过为react-native-fetch-blob(在我的例子中为rn-fetch-blob)创建一个模拟来解决这个问题。 The mock suggested here worked for me: https://github.com/wkh237/react-native-fetch-blob/issues/212#issuecomment-340706825
这里建议的模拟对我有用 : https : //github.com/wkh237/react-native-fetch-blob/issues/212#issuecomment-340706825
As @scerelli I also wanted the mock to happen in a separate place so it can be used by all tests.
作为@scerelli我也希望模拟在一个单独的地方发生,所以它可以被所有测试使用。 Placing the following code in / mocks /react-native-fetch-blob.js seems to work for me:
将以下代码放在/ mocks /react-native-fetch-blob.js中似乎对我有用:
const existsMock = jest.fn();
const existsMock = jest.fn(); existsMock.mockReturnValueOnce({then: jest.fn()});
existsMock.mockReturnValueOnce({then:jest.fn()});
export default {
DocumentDir: () => {},
ImageCache: {
get: {
clear: () => {},
},
},
fs: {
exists: existsMock,
dirs: {
MainBundleDir: () => {},
CacheDir: () => {},
DocumentDir: () => {},
},
},
};
It's a variation of @SoundBlaster solution, just in a global mock file.
它是@SoundBlaster解决方案的变体,仅在全局模拟文件中。 With that I don't need to import or implicitly mock anything in the test code.
有了它,我不需要导入或隐式模拟测试代码中的任何内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.