简体   繁体   English

React Jest - TypeError: (0, _[....]) 不是 function

[英]React Jest - TypeError: (0 , _[....]) is not a function

I'm trying to create unit tests with Jest for a React application and unfortunelly getting errors after importing any component into my tests.我正在尝试使用 Jest 为 React 应用程序创建单元测试,不幸的是,在将任何组件导入我的测试后出现错误。 The error appears on every function that I have.该错误出现在我拥有的每个 function 上。 The application is created with Create React App and this problem repeats on every function that I have on my project.该应用程序是使用 Create React App 创建的,这个问题在我项目中的每个 function 上都会重复出现。

index.js index.js

import 'react-app-polyfill/ie11';
import React from 'react';
import ReactDOM from 'react-dom';
import * as serviceWorker from './serviceWorker';
import Settings from './AppEnvironment';
import intl from 'react-intl-universal';
import { Provider } from 'mobx-react';
import { stores } from '~/stores';
import { HashRouter as Router } from 'react-router-dom';
import 'antd/dist/antd.css';
import './assets/stylesheets/styles.scss';
import App from './App';
import ReactBreakpoints from 'react-breakpoints';
import { breakpoints } from '~/utils/BreakPoints';
import AppProvider from './hooks';
import { initialize } from '@TruckPad/truckpad-analytics';
import { initializeGTM, initializeBlip } from '~/services/external';
import { resetLogin } from './utils/Utils';

initialize([Settings.gtag, Settings.utag]);

initializeGTM();
initializeBlip(
  'YXRoZW5hcm91dGVyOjIxN2U2ODdlLTk3MTUtNDE0NC04ZjJiLTNkNTZhNmQ1MGY1NQ==',
  Settings.environment === 'production',
);

const locales = {
  'pt-BR': require('./locales/pt-BR.json'),
  'en-US': require('./locales/en-US.json'),
};
const currentLocale = locales[navigator.language] ? navigator.language : 'pt-BR';

intl.init({
  currentLocale,
  locales,
});

console.log('Environment: ' + Settings.environment);
console.log('Version: ' + Settings.version);
resetLogin();
ReactDOM.render(
  <Provider {...stores}>
    <AppProvider>
      <ReactBreakpoints breakpoints={breakpoints}>
        <Router baseline="/">
          <App />
        </Router>
      </ReactBreakpoints>
    </AppProvider>
  </Provider>,
  document.getElementById('root'),
);

serviceWorker.unregister();

Jest config on package.json package.json 上的笑话配置

  "jest": {
    "moduleNameMapper": {
      "~/(.*)": "<rootDir>/src/",
      "\\.(css|less|scss|sass)$": "identity-obj-proxy"
    }
  }

Error when running yarn test运行纱线测试时出错

 FAIL  src/pages/Shipments/List/ShipmentsList.spec.js
  ● Test suite failed to run

    TypeError: (0 , _Utils.generateUUID) is not a function

      283 | export const stoppingPointEmpty = {
      284 |   action: '',
    > 285 |   key: generateUUID(),
          |        ^
      286 |   address: {
      287 |     street_name: '',
      288 |     street_number: '',

      at Object.<anonymous> (src/components/Commons.js:285:8)
      at Object.<anonymous> (src/stores/VehicleStore.js:5:1)
      at Object.<anonymous> (src/utils/GenericClass.js:4:1)
      at Object.<anonymous> (src/App.js:14:1)
      at Object.<anonymous> (src/index.js:12:1)
      at Object.<anonymous> (src/pages/Shipments/List/ShipmentsList.js:3:1)
      at Object.<anonymous> (src/pages/Shipments/List/ShipmentsList.spec.js:2:1)

This function (generateUUID) and every other function with export const is giving me the same errors.这个 function (generateUUID) 和其他所有带有export const的 function 都给了我同样的错误。

Anyone here ever have the same problem?这里有人遇到过同样的问题吗? I think probably is an babel config or something like that.我认为可能是一个 babel 配置或类似的东西。 Thanks for the help谢谢您的帮助

I had the same weird issue… In my case I had a file called feature.js just next to real file I wanted to import feature.ts .我遇到了同样奇怪的问题……在我的情况下,我有一个名为feature.js的文件,就在我想要导入的真实文件旁边feature.ts This file probably appeared when I ran tsc by mistake (or webstorm did it for me…)这个文件可能是我错误运行 tsc 时出现的(或者 webstorm 为我做的......)

In my understanding, jest tried to import the .js file instead of the .ts one据我了解,jest 试图导入.js文件而不是.ts文件

Removing feature.js fixed the problem.删除feature.js解决了这个问题。 PS: I am running TS 4.5. PS:我正在运行 TS 4.5。

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

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