Jest 在 React Native 中给出了以下内容:“Jest 给出了以下内容:SyntaxError:意外的标记‘导出’”

问题描述 投票:0回答:1

在我的反应本机应用程序项目中,我尝试在登录页面屏幕上运行测试,仅测试其中元素的渲染。我得到了什么:

Details:

    C:\Users\Hp\OneDrive\Desktop\testing with jest\testingJest\node_modules\@fortawesome\react-native-fontawesome\index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { default as FontAwesomeIcon } from './dist/components/FontAwesomeIcon'
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

       8 |   StyleSheet,
       9 | } from 'react-native';
    > 10 | import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome';
         | ^
      11 | import {faPhone} from '@fortawesome/free-solid-svg-icons';
      12 | import {faEye} from '@fortawesome/free-solid-svg-icons';
      13 | import Svg, {Path} from 'react-native-svg';

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
      at Object.require (screens/LoginScreen.js:10:1)
      at Object.require (__tests__/Login.test.tsx:3:1)

这是测试代码 Login.test.tsx

import React from 'react';
import {render} from '@testing-library/react-native';
import LoginScreen from '../screens/LoginScreen';

describe('LoginScreen', () => {
  test('renders correctly', () => {
    const {getByTestId, getByPlaceholderText, getByText} = render(
      <LoginScreen />,
    );
    const container = getByTestId('login-container');
    expect(container).toBeTruthy();

    const textConnection = getByText('Connexion');
    expect(textConnection).toBeTruthy();

    const placeTextPhone = getByPlaceholderText('Phone Number');
    expect(placeTextPhone).toBeTruthy();

    const placePassword = getByPlaceholderText('Password');
    expect(placePassword).toBeTruthy();
  });
});

还有 jest.config.js :

module.exports = {
   preset: 'react-native',
};

还有 babel.config.js :

module.exports = {
   presets: ['module:@react-native/babel-preset'],
};

我跑步

yarn test
期待收到测试通过

react-native jestjs babeljs font-awesome
1个回答
0
投票

我在测试代码中添加了一个用于 fontawesome 的模拟,并且它起作用了:

jest.mock('@fortawesome/react-native-fontawesome', () => ({ FontAwesomeIcon: '', }));
© www.soinside.com 2019 - 2024. All rights reserved.