无法用摩纳哥编辑器测试玩笑 - 意外的标记
Cannot test jest with monaco editor - unexpected token
运行 应用程序开玩笑失败:
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)
应用程序已安装 jest
和 babel-jest
的软件包。
Babel 配置:
const presets = [
[
"@babel/env",
{
targets: {
edge: "17",
firefox: "60",
chrome: "67",
safari: "11.1"
},
useBuiltIns: "usage",
corejs: 3,
}
],
"@babel/preset-react"
];
const plugins = [
"@babel/plugin-proposal-object-rest-spread",
"@babel/plugin-proposal-class-properties",
"babel-plugin-styled-components"
];
module.exports = { presets, plugins };
从 monaco 延迟加载模块的文档中建议的 import 语句导致 esm 文件夹,jest 不熟悉。
import * as monaco from "monaco-editor/esm/vs/editor/editor.api.js";
默认情况下,babel-jest
不会转换 node_modules
,因此任何引用 monaco-editor
的内容都会出错。一种可能的解决方案是通过 transformIgnorePatterns
as mentioned in the docs
将 monaco-editor
包包含到编译步骤中
将这些添加到 jest 配置中:
{
"transformIgnorePatterns": [
"node_modules\/(?!(monaco-editor)\/)"
]
}
PS:如果您使用的是 jest-dom
,它可能会开始抱怨未实现 monaco-editor
的某些功能,您可以通过以下方式模拟它:
jest.mock("monaco-editor/esm/vs/editor/editor.api.js");
我对 Jest 和 Monaco 也有同样的问题,为了让测试通过,我必须:
- 为 monaco-editor 添加一个 moduleNameMapper:#133 (comment)
- 配置 setupTests.js 就像这里解释的那样:
我正在使用:
- "jest": "^24.9.0"
- "babel-jest": "24.9",
- "monaco-editor": "^0.20.0"
- "react-monaco-editor": "0.33.0",
- "monaco-editor-webpack-plugin": "^1.8.2",
唯一帮助我解决该问题的解决方法(来自 here):
在文件夹 __mocks__
中创建文件 react-monaco-editor.js
,内容为:
import * as React from 'react';
export default function MonacoEditor() {
return <div />;
}
运行 应用程序开玩笑失败:
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)
应用程序已安装 jest
和 babel-jest
的软件包。
Babel 配置:
const presets = [
[
"@babel/env",
{
targets: {
edge: "17",
firefox: "60",
chrome: "67",
safari: "11.1"
},
useBuiltIns: "usage",
corejs: 3,
}
],
"@babel/preset-react"
];
const plugins = [
"@babel/plugin-proposal-object-rest-spread",
"@babel/plugin-proposal-class-properties",
"babel-plugin-styled-components"
];
module.exports = { presets, plugins };
从 monaco 延迟加载模块的文档中建议的 import 语句导致 esm 文件夹,jest 不熟悉。
import * as monaco from "monaco-editor/esm/vs/editor/editor.api.js";
默认情况下,babel-jest
不会转换 node_modules
,因此任何引用 monaco-editor
的内容都会出错。一种可能的解决方案是通过 transformIgnorePatterns
as mentioned in the docs
monaco-editor
包包含到编译步骤中
将这些添加到 jest 配置中:
{
"transformIgnorePatterns": [
"node_modules\/(?!(monaco-editor)\/)"
]
}
PS:如果您使用的是 jest-dom
,它可能会开始抱怨未实现 monaco-editor
的某些功能,您可以通过以下方式模拟它:
jest.mock("monaco-editor/esm/vs/editor/editor.api.js");
我对 Jest 和 Monaco 也有同样的问题,为了让测试通过,我必须:
- 为 monaco-editor 添加一个 moduleNameMapper:#133 (comment)
- 配置 setupTests.js 就像这里解释的那样:
我正在使用:
- "jest": "^24.9.0"
- "babel-jest": "24.9",
- "monaco-editor": "^0.20.0"
- "react-monaco-editor": "0.33.0",
- "monaco-editor-webpack-plugin": "^1.8.2",
唯一帮助我解决该问题的解决方法(来自 here):
在文件夹 __mocks__
中创建文件 react-monaco-editor.js
,内容为:
import * as React from 'react';
export default function MonacoEditor() {
return <div />;
}