React native SyntaxError:意外标识符-反应酶测试中的无意义错误

React native SyntaxError:意外标识符-反应酶测试中的无意义错误,react-native,enzyme,React Native,Enzyme,我正在React Native中开发,并尝试按照上的指导设置jest测试 我想我把一切都安排好了。这是我的配置: //setup-tests.js 导入“react native”; 进口“玩笑酶”; 从“酶-适配器-反应-16”导入适配器; 从“酶”进口酶; //在node.js环境中设置要装载到的酶的DOM const{JSDOM}=require(“JSDOM”); const jsdom=new jsdom(“”); const{window}=jsdom; 功能copyProps(sr

我正在React Native中开发,并尝试按照上的指导设置jest测试

我想我把一切都安排好了。这是我的配置:

//setup-tests.js
导入“react native”;
进口“玩笑酶”;
从“酶-适配器-反应-16”导入适配器;
从“酶”进口酶;
//在node.js环境中设置要装载到的酶的DOM
const{JSDOM}=require(“JSDOM”);
const jsdom=new jsdom(“”);
const{window}=jsdom;
功能copyProps(src、target){
对象。定义属性(目标{
…Object.getOwnPropertyDescriptors(src),
…Object.getOwnPropertyDescriptors(目标)
});
}
global.window=窗口;
global.document=window.document;
global.navigator={
userAgent:“node.js”
};
复制道具(窗口、全局);
//将Ezyme设置为装载到DOM,模拟事件,并在测试中检查DOM。
configure({adapter:newadapter()});
/*忽略一些预期的警告
*见:https://jestjs.io/docs/en/tutorial-react.html#snapshot-使用mocks-enzyme-and-react-16进行测试
*看https://github.com/Root-App/react-native-mock-render/issues/6
*/
const originalConsoleError=console.error;
console.error=消息=>{
if(message.startsWith(“警告:”)){
返回;
}
原始解决方案错误(消息);
};
//jest.config.js
module.exports={
setupfileafterenv:“setup tests.js”
};
//Test.Test.js
从“React”导入React;
从“反应测试渲染器”导入渲染器;
从“酶”导入{mount,ReactWrapper};
从“react redux”导入{Provider};
从“react native”导入{Text};
从“../js/LoginView”导入LoginView
稍后您将看到
LoginView
的顶部

您会注意到,我甚至没有在测试文件中编写任何测试,我只是试图让jest运行到可以评估测试的程度

如果没有导入LoginView,jest将运行并“失败”,因为我的测试套件必须至少包含一个测试。听起来不错

添加到
import LoginView
,我得到了这个错误(在
LoginView
顶部显示了import语句):

    import Button from './buttons/Button';
           ^^^^^^

    SyntaxError: Unexpected identifier

      1 | import React, { Component } from "react";
    > 2 | import { Input, Button, Image } from "react-native-elements";
        | ^
      3 | import { Text, View } from "react-native";
      4 | import { connect } from "react-redux";
      5 | import F8StyleSheet from "./F8StyleSheet";

      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:471:17)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:513:25)
      at Object.<anonymous> (js/LoginView.js:2:1)
这里是新的错误

 FAIL  tests/Test.test.js
  ● Test suite failed to run

    Cannot find module './Platform' from 'Dimensions.js'

    However, Jest was able to find:
        './Platform.android.js'
        './Platform.ios.js'

    You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node'].

    See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string

    However, Jest was able to find:
        '../Utilities/Dimensions.js'

    You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node'].

    See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string

    However, Jest was able to find:
        'buttons/Button.js'

    You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node'].

    See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string

    However, Jest was able to find:
        '../js/LoginView.js'

    You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node'].

    See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:229:17)
      at Object.require (node_modules/react-native/Libraries/Utilities/Dimensions.js:14:18)

react原生元素
包含ES6代码

ES6代码需要先编译,然后才能由
Jest
运行

默认情况下,
Jest
不会编译
node\u模块中的任何内容,但是
react native
有一个
Jest预设

要包含
react-native元素
,您还需要告诉
Jest
通过以下方式进行编译:


我有一个类似的问题,报告说,您是否设法解决了您的问题,或者您仍然被卡住?这可能是与其他一些软件包的兼容性问题。。。
 FAIL  tests/Test.test.js
  ● Test suite failed to run

    Cannot find module './Platform' from 'Dimensions.js'

    However, Jest was able to find:
        './Platform.android.js'
        './Platform.ios.js'

    You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node'].

    See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string

    However, Jest was able to find:
        '../Utilities/Dimensions.js'

    You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node'].

    See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string

    However, Jest was able to find:
        'buttons/Button.js'

    You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node'].

    See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string

    However, Jest was able to find:
        '../js/LoginView.js'

    You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node'].

    See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:229:17)
      at Object.require (node_modules/react-native/Libraries/Utilities/Dimensions.js:14:18)
transformIgnorePatterns: [
  'node_modules/(?!(jest-)?react-native|@react-native-community|react-native-elements)',
]