【发布时间】:2021-03-04 12:05:15
【问题描述】:
我在我的 Angular 10 应用程序中使用 CKEditor5。经过漫长而痛苦的集成过程,CKEditor 在应用程序中完美运行。该应用程序的结构是一个 nx monorepo,如果这有影响的话。
但是,我现在无法为我的应用运行某些单元测试。任何使用 CKeditor 的组件(目前大约 5 个组件)的单元测试现在都会失败,并出现以下错误:
C:\path\to\my\repo\node_modules@ckeditor\ckeditor5-watchdog\src\editorwatchdog.js:12
从'lodash-es'导入{throttle,cloneDeepWith,isElement};
^^^^^^
SyntaxError: 不能在模块外使用 import 语句
看来我需要使用 Babel 来转换 editorwatchdog.js 中的导入,以便 Jest 可以理解它们。为了解决这个问题,我安装了@babel/core 7.13.8、@babel/preset-env 7.13.9 和 babel-jest 26.6.3(注意,我已经安装了 jest 26.2.2)
我在项目的根目录下添加了一个 babel.config.js 文件,其中仅包含以下内容:
module.exports = {
presets: [
['@babel/preset-env', { targets: { node: 'current' } }],
],
};
最后,我更新了我的 jest.config.js 根文件以包含 transform 和 transformIgnorePatterns 键:
module.exports = {
transform: {},
transformIgnorePatterns: "\\\\/node_modules\\\\/(?!@ckeditor).+\\.(js|jsx|ts|tsx)$",
projects: [
... // all my libs and apps
]
}
(注意:我也尝试将 transform 和 transformIgnorePatterns 添加到 package.json)
但是当我使用上述更改运行测试时,我仍然看到与以前相同的错误消息。显然我错过了什么,但是什么?
最终结果是我需要 Babel 仅转换 @ckeditor 文件,并且仅在运行单元测试时。
【问题讨论】:
标签: jestjs babeljs angular10 ckeditor5 babel-jest