问题测试的反应组件,以开玩笑实现反应叶片图

发布于 2025-02-10 22:06:00 字数 1565 浏览 1 评论 0原文

当我尝试测试实现React Leaflet库的React组件时,我会有以下问题,

    C:\digital-booking-ui\node_modules\react-leaflet\lib\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { useMap, useMapEvent, useMapEvents } from './hooks.js';
                                                                                  ^^^^^^

SyntaxError: Unexpected token 'export'

  1 | import React from "react";
  2 | import { makeStyles } from "@material-ui/core";
> 3 | import { MapContainer, TileLayer, Marker, Popup, useMap } from "react-leaflet";
    | ^
  4 |
  5 | const Map = () => {
  6 |   const classes = useStyles();

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
  at Object.<anonymous> (src/components/accomodation/Map.js:3:1)

我在Internet上搜索了该问题,并且我发现的建议对我不起作用。

当我尝试渲染与该库有关系的任何组件时,会发生此错误,例如,app.test.js,

import { render, screen, prettyDOM } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect'
import App from './App';
import { BrowserRouter } from 'react-router-dom';
import { ThemeProvider } from '@material-ui/core';
import theme from "./theme";

let component = null;

beforeEach(() => {
    component = render(
      <BrowserRouter>
        <ThemeProvider theme={theme}>
          <App />
        </ThemeProvider>
      </BrowserRouter>
    );
}
);

test('render App', () => {
  expect(component).toBeTruthy();
});

我该如何解决?这似乎是一个问题,因为没有意识到组件的出口

I have the following issue when I tried to test a react components that implement the react-leaflet library

    C:\digital-booking-ui\node_modules\react-leaflet\lib\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { useMap, useMapEvent, useMapEvents } from './hooks.js';
                                                                                  ^^^^^^

SyntaxError: Unexpected token 'export'

  1 | import React from "react";
  2 | import { makeStyles } from "@material-ui/core";
> 3 | import { MapContainer, TileLayer, Marker, Popup, useMap } from "react-leaflet";
    | ^
  4 |
  5 | const Map = () => {
  6 |   const classes = useStyles();

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
  at Object.<anonymous> (src/components/accomodation/Map.js:3:1)

I search the problem on the internet and the recommendations I found don't work for me.

This error happen when I tried to render any component that has a relation with that library, for example, App.test.js

import { render, screen, prettyDOM } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect'
import App from './App';
import { BrowserRouter } from 'react-router-dom';
import { ThemeProvider } from '@material-ui/core';
import theme from "./theme";

let component = null;

beforeEach(() => {
    component = render(
      <BrowserRouter>
        <ThemeProvider theme={theme}>
          <App />
        </ThemeProvider>
      </BrowserRouter>
    );
}
);

test('render App', () => {
  expect(component).toBeTruthy();
});

How could I fix this? It seems a problem of Jest not recognizing the export of the components

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抠脚大汉 2025-02-17 22:06:00

在您的jest.config.js中添加这些行,

"jest": {   
    "moduleNameMapper": {
      "react-leaflet": "<rootDir>/mocks/reactLeafletMock.js"
    }
  }

然后在“模拟”文件夹(或任何您想要的任何内容)中添加reactleafleafletmock.js模块,该模块返回一个空对象

module.exports = {}

应该可以的(它对我有用) )

您最终可以在对象中添加“反应叶片”钩子,以避免开玩笑中的其他错误

module.exports = {
    useMapEvents: () => {}
}

In your jest.config.js add these lines

"jest": {   
    "moduleNameMapper": {
      "react-leaflet": "<rootDir>/mocks/reactLeafletMock.js"
    }
  }

Then in a "mocks" folder (or whatever you want) add a reactLeafletMock.js module that return an empty object

module.exports = {}

It should be ok (it worked for me)

You could eventually add "react-leaflet" hooks within the object to avoid other errors from Jest

module.exports = {
    useMapEvents: () => {}
}
单挑你×的.吻 2025-02-17 22:06:00

我已经解决了我的测试文件中嘲笑反应叶的问题

jest.mock('react-leaflet', () => jest.fn());

I've solved this problem with mocking react-leaflet in my test file

jest.mock('react-leaflet', () => jest.fn());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文