Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't run unit test same as in the example docs #6401

Closed
cddumanov opened this issue Apr 15, 2020 · 9 comments
Closed

Can't run unit test same as in the example docs #6401

cddumanov opened this issue Apr 15, 2020 · 9 comments

Comments

@cddumanov
Copy link

cddumanov commented Apr 15, 2020

When I run my test I see the next:
image

Code of the test is pretty mush simple.

import React from 'react'
import { shallow } from 'enzyme';
import OrganizationSelect from './index';

it('renders Dashboard', () => {
  // Use the wrapped components
  const wrapper = shallow(<OrganizationSelect.WrappedComponent user={{ list: [] }} loading={{user: []}}/>);
  // const wrapper = shallow(<OrganizationSelect/>);
  expect(wrapper).toBe()
});

Code of the component

import React from 'react';
import { connect } from 'umi';
import { Select } from 'antd';
import styles from './index.less';

const { Option } = Select;

const OrganizationSelect = ({ dispatch, currentUser, loading, ...props }) => {
  const handleChange = async (code) => {
    await dispatch({
      type: 'user/setLastSelectedOrganization',
      payload: code,
    });
    await dispatch({
      type: 'user/fetchCurrent',
    });
  };
  if (!loading) {
    return (
      <Select
        className={styles.organizationSelect}
        defaultValue={
          currentUser.last_selected_organization
            ? currentUser.last_selected_organization.code
            : null
        }
        onChange={handleChange}
      >
        {currentUser.organizations.map(({ name, code }, i) => (
          <Option key={code} value={code}>
            {name}
          </Option>
        ))}
      </Select>
    );
  }
  return <></>;
};

export default connect(({ user, loading }) => ({
  currentUser: user.currentUser,
  loading: loading.models.user,
}))(OrganizationSelect);

How I can fix this error and make my test run? Can you help me, please? Many thanks for the answers in English!

@cddumanov
Copy link
Author

@chenshuai2144 @afc163 Please help.

@zsxing99
Copy link

Having the same issue.

  ● Test suite failed to run

    TypeError: (0 , _umi.connect) is not a function

umi version:

"umi": "^3.2.3",

@zsxing99
Copy link

Update: no solution to this. Problem with umi dependency.

@y-lohse
Copy link

y-lohse commented Sep 21, 2020

Ran into the same problem, the issue was that umi is importing things from @@/whatever and jest doesn't know the @@ alias.
Adding this in the jest configuration solved the problem:

moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/src/$1',
    '^@@/(.*)$': '<rootDir>/src/.umi/$1',
  },

@1765659645
Copy link

@y-lohse
Hello,I tested it according to your method. There is no problem in my local area, but after submitting to git, it will go through the unit test of Jenkins, but the code on git does not have a .umi directory, so there are other feasible methods Is it

@y-lohse
Copy link

y-lohse commented Sep 25, 2020

I think the .umi directory is created when running the build command, so one possible workaround would be to build the project before running the tests?

@1765659645
Copy link

@y-lohse
ok,My current solution is to extract connect to another file
image

@Lubyam
Copy link

Lubyam commented Dec 5, 2020

the same issue.
Unhandled Rejection (TypeError): (0 , _umi.connect) is not a function
./src/layouts/BasicLayout.tsx
src/layouts/BasicLayout.tsx:174

171 | );
172 | };
173 |
174 | export default connect(({ global, settings }: ConnectState) => ({
175 | collapsed: global.collapsed,
176 | settings,
177 | }))(BasicLayout);
image

@chenshuai2144
Copy link
Collaborator

我们很关注这个问题,但是它已经很长时间没有回应。我们会暂时关闭它
——
We are very concerned about this issue, but it has not responded for a long time. We will temporarily close it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants