简体   繁体   English

开玩笑测试无法使用 create react app 和 craco 导入铯

[英]jest test cannot import cesium using create react app and craco

I am using create react app with craco, and craco-cesium to load cesium into my project.我正在使用带有 craco 和 craco-cesium 的 create react 应用程序将 cesium 加载到我的项目中。 I am trying to setup jest to start creating tests but the issue is Cesium is using requireJS.我正在尝试设置 jest 以开始创建测试,但问题是 Cesium 正在使用 requireJS。

I added the following to my package.json我将以下内容添加到我的package.json

  // package.json

  ...
  "jest": {
    "collectCoverageFrom": [
      "src/**/*.{js,jsx,ts,tsx}', '!src/**/*.d.ts"
    ],

    "globalSetup": "<rootDir>/setupTests.js"
  }
  ...

based on the ticket opened up here: https://github.com/facebook/jest/issues/1914基于此处打开的票证: https://github.com/facebook/jest/issues/1914

I setup the setupTests.js with the following code:我使用以下代码设置setupTests.js

// setupTests.js
module.exports = async () => {
  var cesium = require('cesium');
  process.$Cesium = cesium
};

and I have the first basic test for valid rendering:我对有效渲染进行了第一个基本测试:

// App.test.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

it('renders without crashing', () => {
  const div = document.createElement('div');
  ReactDOM.render(<App />, div);
  ReactDOM.unmountComponentAtNode(div);
});

however I get the error in one of my view inside <App /> component with the following code:但是,我在<App />组件中的一个视图中使用以下代码出现错误:

import Cesium from "cesium";
...
TypeError: Cannot read property 'Ion' of undefined
...
Cesium.Ion.defaultAccessToken = '...';

reporting Cesium as undefined when calling it's functions.在调用它的函数时报告 Cesium 为未定义。 I tried to use jest.config.js for jest to pick up the configuration from there with the following code:我尝试使用jest.config.js来开玩笑,使用以下代码从那里获取配置:

// jest.config.js
const { createJestConfig } = require("@craco/craco");

const cracoConfig = require("../craco.config.js");
const jestConfig = createJestConfig(cracoConfig);

module.exports = jestConfig;

but create react app doesn't pick up this file so I cannot try this to verify if this would work.但是创建反应应用程序不会获取此文件,因此我无法尝试验证这是否可行。

encoutered the same problem.遇到了同样的问题。

Upgrading react-scripts to version 4.0.1 and @craco/craco@ to version 6.0.0, seemed to solve these problems, and jest now recognizes all imports from cesium.react-scripts升级到 4.0.1 版和将@craco/craco@升级到 6.0.0 版,似乎解决了这些问题,并且 jest 现在可以识别所有来自 cesium 的导入。 No other configuration files were needed.不需要其他配置文件。

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

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