Jestjs 无法使用编辑器测试jest-意外标记
在应用程序上运行jest失败,原因是:Jestjs 无法使用编辑器测试jest-意外标记,jestjs,monaco-editor,Jestjs,Monaco Editor,在应用程序上运行jest失败,原因是: Details: /home/**/node_modules/monaco-editor/esm/vs/editor/editor.api.js:5 import { EDITOR_DEFAULTS } from './common/config/editorOptions.js'; ^ SyntaxError: Unexpected token { > 1 | import * as monaco from "monaco-ed
Details:
/home/**/node_modules/monaco-editor/esm/vs/editor/editor.api.js:5
import { EDITOR_DEFAULTS } from './common/config/editorOptions.js';
^
SyntaxError: Unexpected token {
> 1 | import * as monaco from "monaco-editor/esm/vs/editor/editor.api.js";
| ^
2 |
3 | /**
4 | * Get create function for the editor.
at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
at Object.<anonymous> (src/utils/editor-actions.js:1:1)
摩纳哥懒散加载模块文档中建议的导入语句指向esm文件夹,jest对此并不熟悉
import * as monaco from "monaco-editor/esm/vs/editor/editor.api.js";
默认情况下,babeljest
不会转换node\u模块
,因此任何引用monaco编辑器的内容都会出错。一种可能的解决方案是将摩纳哥编辑器
包包含到编译步骤中,如中所述
将以下内容添加到jest配置:
{
"transformIgnorePatterns": [
"node_modules\/(?!(monaco-editor)\/)"
]
}
PS:如果您正在使用,它可能会开始抱怨没有从摩纳哥编辑器
中实现某些功能,您可以通过以下方式模拟它:
jest.mock("monaco-editor/esm/vs/editor/editor.api.js");
我对Jest和摩纳哥也有同样的问题,为了通过测试,我必须:
为编辑器添加moduleNameMapper:
配置setupTests.js,如下所述:
我正在使用:
- “笑话”:“^24.9.0”
- “巴别塔笑话”:“24.9”
- “摩纳哥编辑”:“^0.20.0”
- “编辑”:“0.33.0”
- “摩纳哥编辑器网页包插件”:“^1.8.2”
在这个问题上帮助我的唯一解决方法(来自:
在文件夹\uuuu mocks\uuuu
中创建文件编辑器.js
,内容如下:
import*as React from'React';
导出默认函数MonacoEditor(){
返回;
}
关于transformIgnorePatterns和moduleNameMapper,我尝试了多种解决方案,但唯一对我有效的是模仿摩纳哥。
jest.mock("monaco-editor/esm/vs/editor/editor.api.js");