我有一个使用 mxgraph 包的 Vue JS 组件:
https://www.npmjs.com/package/mxgraph
并像这样导入它:
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';
这是我的 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"
}
}
};
当我运行测试时,我得到:
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)
当我使用普通的 webpack 配置文件运行我的应用程序时,导入工作正常。我需要在 jest.config 中添加任何内容来修复此错误吗?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
mxGraphnpm 包一直都是一团糟。请注意,mxGraph 的后继者
maxGraph将处理它: https://github.com/maxGraph/maxGraph由于您正在使用 Vue 并且您的代码片段表明您正在使用 TypeScript,因此我建议您使用 https://github.com/typed-mxgraph/typed-mxgraph。您将获得 TypeScript 类型和 mxGraph npm 集成的完整工作解决方案。自述文件提供了全面的使用示例和演示。 在 jest 测试中使用 mxGraph 时,根据您正在运行的 mxGraph 部分,您将需要使用 相关测试中的jsdom环境。这是因为 mxGraph 使用
window对象,该对象在node环境中不可用。如果您仍然想保留 mxGraph 集成的 js 方式,您可以查看 https://github.com/jgraph/mxgraph/issues/175#issuecomment-482008896 或其他堆栈溢出问题(它们很多)。