简体   繁体   English

如何在测试期间使用 jest 导入模块?

[英]How to import modules during tests using jest?

I'm using React, Enzyme and Jest.我正在使用 React、Enzyme 和 Jest。 This is my connect component which only renders a button这是我的连接组件,它只呈现一个按钮

import { connect } from 'react-redux';
import { withStyles } from '@material-ui/core/styles';

import PlaidLinkButton from 'react-plaid-link-button';


const plaidEnv = process.env.REACT_APP_PLAID_ENV

export class ConnectStep extends Component {

  handleOnSuccessBroker = async (token, metadata) => {
    //unimportant
  };

  render() {
    const { classes } = this.props;
    return (
        <PlaidLinkButton
          buttonProps={{
            className: classes.connectButton,
            id: 'button',
          }}
          plaidLinkProps={{
            clientName: '###',
            key: '###',
            env: plaidEnv,
            product: ['transactions'],
            onSuccess: this.handleOnSuccessBroker,
            token: ''
          }}
        >
          {this.props.plaid.accounts.length > 0 ? 'CONNECTED' : 'Start'}
            </PlaidLinkButton>
    );
  }
}

As you can see Im importing PlaidLinkButton but jest throws this error:正如您所看到的,我正在导入 PlaidLinkBut​​ton 但开玩笑会引发此错误:


    ###/node_modules/react-plaid-link-button/dist/react-plaid-link-button/react-plaid-link-button.js:19
    import PropTypes from 'prop-types';
           ^^^^^^^^^

    SyntaxError: Unexpected identifier

      4 | 
      5 | import { setPlaid } from '../../../actions/actions';
    > 6 | import PlaidLinkButton from 'react-plaid-link-button';

What am I missing?我错过了什么? I made successful test suites for other components that also import modules.我为也导入模块的其他组件制作了成功的测试套件。 But this one in particular is giving me problems.但是这个特别给我带来了问题。

    import PropTypes from 'prop-types';
           ^^^^^^^^^

    SyntaxError: Unexpected identifier

This is likely an issue related to your Jest/Babel config because the modules aren't getting compiled correctly.这可能是与您的 Jest/Babel 配置相关的问题,因为模块未正确编译。 You'd need to remove anywhere you set the Babel option modules to false .您需要删除将 Babel 选项模块设置为false任何地方。

Related: https://github.com/facebook/react/issues/14399相关: https : //github.com/facebook/react/issues/14399

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

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