我正在尝试添加Cypress -angular-unit-test插件到Cypress.
但是,按照相关GitHub页面上提供的说明,我得到以下错误:
The plugins file is missing or invalid.
/MyProjectName/cypress/plugins/index.js:1
import * as cypressTypeScriptPreprocessor from 'cy-ts-preprocessor';
^^^^^^
SyntaxError: Cannot use import statement outside a module
再次,按照指示,我添加了以下内容来支持/index.js
// core-js 3.*
require('core-js/es/reflect');
// core-js 2.*
require('core-js/es7/reflect');
require('cypress-angular-unit-test/support');
和下面的插件/index.js
import * as cypressTypeScriptPreprocessor from 'cy-ts-preprocessor';
module.exports = (on, config) => {
on('file:preprocessor', cypressTypeScriptPreprocessor);
require('@cypress/code-coverage/task')(on, config);
return config;
};
我还将相关的helper.js和cy-ts-preprocessor.js文件添加到plugins文件夹中。
我还添加了必要的配置到cypress.json
"experimentalComponentTesting": true,
我甚至尝试通过添加一个tsconfig来添加类型/节点。Json到cypress文件夹,如下所示:
{
"compilerOptions": {
"target": "es5",
"lib": ["es5", "dom"],
"types": ["cypress", "node"],
"experimentalDecorators": true
},
"include": [
"**/*.ts"
]
}
将目标和/或库值更改为es6没有效果。
将导入更改为require会产生一个不同的错误,即使它确实有效,对于我缺少的一些细节来说,这似乎是一个糟糕的解决方案。
¯_()_/¯
不完全是一个答案,但至少是一个解决方法。
- 重新启动或卸载Cypress和Cypress -angular-unit-test
- 按照此处 的说明安装Nx Cypress
- 返回到cypress-angular-unit-test指令并忽略这部分:
Configure cypress/plugins/index.js to transpile Angular code.
import * as cypressTypeScriptPreprocessor from './cy-ts-preprocessor';
module.exports = (on, config) => {
on('file:preprocessor', cypressTypeScriptPreprocessor);
return config;
};
The file cy-ts-preprocessor is here