allure-cypress 3.0.7+解析index.js时出现Unexpected token错误
升级allure-cypress至3.0.7+后Unexpected token语法错误的解决方法
问题背景
将allure-cypress插件升级到3.0.7及更高版本后,执行Cypress+Cucumber测试脚本时触发语法错误:解析C:\Users\USER\node_modules\allure-cypress\dist\cjs\index.js文件第1行16307位时遇到Unexpected token。3.0.5及以下版本可正常运行,因新版本修复了测试报告Test body下步骤展示的问题,必须完成升级。
在e2e.ts中通过以下方式引入插件:
import "allure-cypress"
环境依赖版本
{ "cucumber": "latest", "@types/node": "latest", "allure-cypress": "^3.0.9", "typescript": "^5.4.5", "cypress": "^13.17.0", "@badeball/cypress-cucumber-preprocessor": "^20.0.6" }
已尝试操作
- 在
tsconfig.json的include字段中添加allure-cypress相关路径及.js文件支持:"include": [ "/*.ts", "/*.js", "./node_modules/cypress", "./node_modules/allure-cypress/dist/cjs", "./node_modules/allure-cypress" ] - 升级/调整package.json中相关依赖版本排查兼容性
错误堆栈信息
Oops...we found an error preparing this test file: > cypress\support\e2e.ts The error was: SyntaxError: Unexpected token (1:16307) while parsing C:\Users\USER\allure-cypress\dist\cjs\index.js while parsing file: C:\Users\USER\node_modules\allure-cypress\dist\cjs\index.js at DestroyableTransform.end [as _flush] (C:\Users\USER\node_modules\insert-module-globals\index.js:114:21) at DestroyableTransform.prefinish (C:\Users\USER\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:138:10) at DestroyableTransform.emit (node:events:513:28) at DestroyableTransform.emit (node:domain:489:12) at prefinish (C:\Users\USER\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:619:14) at finishMaybe (C:\Users\USER\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:627:5) at endWritable (C:\Users\USER\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:638:3) at DestroyableTransform.Writable.end (C:\Users\USER\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:594:22) at DestroyableTransform.onend (C:\Users\USER\node_modules\through2\node_modules\readable-stream\lib\_stream_readable.js:577:10) at Object.onceWrapper (node:events:627:28) at DestroyableTransform.emit (node:events:525:35) at DestroyableTransform.emit (node:domain:489:12) at endReadableNT (C:\Users\USER\node_modules\through2\node_modules\readable-stream\lib\_stream_readable.js:1010:12) at processTicksAndRejections (node:internal/process/task_queues:82:21)"
解决方案
1. 验证Node.js版本兼容性
allure-cypress 3.x版本要求Node.js 16.0及以上,执行node -v检查版本,若低于16则升级Node.js到符合要求的版本。
2. 调整tsconfig.json配置
- 确保
allowJs设为true,允许TypeScript编译JavaScript文件 - 修改
include路径,优先引入ES模块版本的allure-cypress:{ "compilerOptions": { "allowJs": true, // 其他原有配置... }, "include": [ "./**/*.ts", "./**/*.js", "./cypress/**/*", "./node_modules/allure-cypress/dist/esm/**/*" ] }
3. 改用ES模块引入插件
将e2e.ts中的引入语句改为直接引用ES模块版本:
import "allure-cypress/dist/esm/index.js"
4. 配置Cypress预处理器排除allure-cypress转译
若使用@badeball/cypress-cucumber-preprocessor,在cypress.config.ts中调整预处理器配置,避免转译allure-cypress文件:
import { defineConfig } from "cypress"; import createBundler from "@bahmutov/cypress-esbuild-preprocessor"; import { addCucumberPreprocessorPlugin } from "@badeball/cypress-cucumber-preprocessor"; import createEsbuildPlugin from "@badeball/cypress-cucumber-preprocessor/esbuild"; export default defineConfig({ e2e: { async setupNodeEvents(on, config) { const bundler = createBundler({ plugins: [createEsbuildPlugin(config)], }); // 跳过allure-cypress文件的预编译 on("file:preprocessor", (file) => { if (file.filePath.includes("allure-cypress")) { return file.filePath; } return bundler(file); }); await addCucumberPreprocessorPlugin(on, config); return config; }, specPattern: "cypress/e2e/**/*.feature", }, });
5. 清理依赖缓存重新安装
rm -rf node_modules package-lock.json npm install
内容的提问来源于stack exchange,提问作者Fredp
相关产品推荐
相关产品推荐

