类型错误:符号不是一个函数。

6

FooterView.tsx

import styles from './Footer.module.scss';

import logo from 'assets/images/logo_header.svg';
import { ReactSVG } from 'react-svg';
import iconTwitter from 'assets/images/logo_twitter.svg';
import iconFacebook from 'assets/images/logo_facebook.svg';
import iconYoutube from 'assets/images/logo_youtube.svg';
import { proxyConfSelector } from 'core';
import { useSelector } from 'react-redux';
import { useTranslation } from 'hooks/useTranslation';

const Footer = () => {
  const proxyConf = useSelector(proxyConfSelector);
  const year = new Date().getFullYear();
  const [t, lang] = useTranslation();

  // eslint-disable-next-line no-console
  console.log('Language', [t, lang]);

  return (
    <div className={styles.footerContainer}>
      <a href={`https://fodjan.com/${lang === 'de' ? 'de/' : 'en/'}`}>
        <ReactSVG className={styles.footerLogo} src={logo} />
      </a>
      <div className={styles.love}>
        Made with <span className={styles.heart}></span> in Germany
      </div>
      <div className={styles.socialMediaContainer}>
        <div className={styles.socialList}>
          <a
            href="https://twitter.com/fodjan?lang=de"
            target="_blank"
            rel="noopener noreferrer"
          >
            <img src={iconTwitter} alt="twitter icon" />
          </a>
          <a
            href="https://www.facebook.com/fodjan.de/"
            target="_blank"
            rel="noopener noreferrer"
          >
            <img src={iconFacebook} alt="facebook icon" />
          </a>
          <a
            href="https://www.youtube.com/channel/UCKwdzpWq1uy0sblX3_VIKHg"
            target="_blank"
            rel="noopener noreferrer"
          >
            <img src={iconYoutube} alt="youtube icon" />
          </a>
        </div>
      </div>
      <div className={styles.links}>
        <a
          href={proxyConf ? proxyConf.fe1Url + '/infos/terms' : ''}
          target="_blank"
          rel="noopener noreferrer"
        >
          {t('AGB')}
        </a>
        <a
          href={proxyConf ? proxyConf.fe1Url + '/infos/policy' : ''}
          target="_blank"
          rel="noopener noreferrer"
        >
          {t('Datenschutz')}
        </a>
        <a
          href={proxyConf ? proxyConf.fe1Url + '/infos/imprint' : ''}
          target="_blank"
          rel="noopener noreferrer"
        >
          {t('Impressum')}
        </a>
      </div>
      <div className={styles.copyright}>
        © {year} fodjan GmbH. {t('Alle Rechte vorbehalten.')}
      </div>
    </div>
  );
};

export default Footer;

FooterView.test.tsx

import FooterView from '../FooterView';
import { render } from '@testing-library/react';
import * as redux from 'react-redux';

const config = {
  fe1Url: 'www.fodjan.com',
  feVersion: '1.0',
};

const spyOnUseSelector = () => {
  jest.spyOn(redux, 'useSelector').mockImplementation(selector => {
    switch (selector.name) {
      case 'proxyConfSelector':
        return config;
      default:
        return jest.fn();
    }
  });
  jest.mock('hooks/useTranslation', () => ({
    useTranslation: jest.fn().mockImplementation(() => [jest.fn(), 'en']),
  }));
};

test('check if rendered correctly', () => {
  spyOnUseSelector();
  const renderCheck = render(<FooterView />);
  expect(renderCheck.container.firstChild).toMatchSnapshot();
});

错误

 TypeError: symbol is not a function

      24 | test('check if rendered correctly', () => {
      25 |   spyOnUseSelector();
    > 26 |   const renderCheck = render(<FooterView />);
         |                       ^
      27 |   expect(renderCheck.container.firstChild).toMatchSnapshot();
      28 | });
      29 | 

我不明白为什么测试失败并收到"TypeError: symbol is not a function"的错误消息。在我看来,似乎有某些东西没有被模拟,但我不确定。
1个回答

8

媒体文件没有被模拟。

创建一个带有以下内容的图像模拟文件:

module.exports = 'test-file-stub';

首先,在根目录下添加一个jest配置文件:

/jest.config.js

文件内容为(如果你已经有了配置文件,只需添加moduleNameMapper对象):

module.exports = {
  moduleNameMapper: {
    '\\.(png|jpg|webp|ttf|woff|woff2|svg|mp4)$': '<rootDir>/path-to-fileMock.js'
  }
}

那应该解决了。

更多信息请参见:https://jestjs.io/docs/webpack


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接