简体   繁体   English

类型错误:运行 Jest 单元测试时 mxgraph 不是函数

[英]TypeError: mxgraph is not a function When Running Jest Unit Tests

I have a Vue JS Component That uses the mxgraph package: https://www.npmjs.com/package/mxgraph我有一个使用mxgraph包的 Vue JS 组件: https : mxgraph

and imports it like this:并像这样导入它:

  import * as mxgraph from 'mxgraph';

  const {
    mxClient,
    mxStackLayout,
    mxGraph,
    mxRubberband,
    mxUtils,
    mxFastOrganicLayout,
    mxSwimlaneLayout,
    mxEvent,
    mxGraphModel,
    mxConstants,
    mxHierarchicalLayout,
    mxSwimlaneManager
  } = mxgraph();
  // noinspection TypeScriptCheckImport
  import {
    mxConstants as MxConstants
  } from 'mxgraph/javascript/mxClient.js'
  import axios from 'axios';

This is my jest.config.js这是我的 jest.config.js

module.exports = {
  preset: "@vue/cli-plugin-unit-jest/presets/no-babel",
  moduleFileExtensions: ["js", "ts", "json", "vue"],
  transform: {
    ".*\\.(vue)$": "vue-jest",
    "^.+\\.tsx?$": "ts-jest"
  },
  globals: {
    "ts-jest": {
      tsConfig: "src/tsconfig.json"
    }
  }
};

When I run my tests I get:当我运行测试时,我得到:

TypeError: mxgraph is not a function

  20 |   import * as mxgraph from 'mxgraph';
  21 | 
> 22 |   const {
     | ^
  23 |     mxClient,
  24 |     mxStackLayout,
  25 |     mxGraph,

  at src/components/task/job/job_pipeline_mxgraph.vue:22:1
  at Object.<anonymous> (src/components/task/job/job_pipeline_mxgraph.vue:568:3)
  at src/components/task/job/task_template_wizard_creation/step_attach_directories_task_template.vue:67:1
  at Object.<anonymous> (src/components/task/job/task_template_wizard_creation/step_attach_directories_task_template.vue:367:3)
  at Object.<anonymous> (tests/unit/task/job/task_template_wizard_creation/step_attach_directories_task_template.spec.js:3:1)

The import works fine when I run my app using my normal webpack configurations files.当我使用普通的 webpack 配置文件运行我的应用程序时,导入工作正常。 Is there anything I need to add to my jest.config to fix this error?有什么我需要添加到我的 jest.config 来修复这个错误的吗?

The mxGraph npm package has always been a mess. mxGraph npm 包一直都是一团糟。

Notice that maxGraph , the mxGraph successor, will handle it: https://github.com/maxGraph/maxGraph请注意, maxGraph的后继者 maxGraph 将处理它: https : //github.com/maxGraph/maxGraph

As you are using Vue and your snippet indicates you are using TypeScript, I suggest you to use https://github.com/typed-mxgraph/typed-mxgraph .当您使用 Vue 并且您的代码段表明您使用的是 TypeScript 时,我建议您使用https://github.com/typed-mxgraph/typed-mxgraph You will get TypeScript types and a fully working solution for the mxGraph npm integration.您将获得 TypeScript 类型和 mxGraph npm 集成的完整解决方案。 The README provides comprehensive usage examples and demos are also available. README 提供了全面的使用示例和演示。 When using mxGraph in jest test, depending on the mxGraph parts you are running, you will need to use the jsdom environment in the related tests.在 jest 测试中使用 mxGraph 时,根据您运行的 mxGraph 部分,您将需要在相关测试中使用jsdom 环境 This is because mxGraph uses the window object, that is not available with the node environment.这是因为 mxGraph 使用window对象,该对象在node环境中不可用。

If you still want to keep the js way for the mxGraph integration, you can have a look at https://github.com/jgraph/mxgraph/issues/175#issuecomment-482008896 or at other stack overflow questions (they are plenty).如果您仍然想保留 mxGraph 集成的 js 方式,您可以查看https://github.com/jgraph/mxgraph/issues/175#issuecomment-482008896或其他堆栈溢出问题(它们很多) .

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

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