You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级ESM后CucumberJS仅显示TypeScript诊断代码问题咨询

问题:ESM模式下Cucumber步骤定义的TypeScript错误仅返回诊断码,无详细信息

我们将测试套件从CommonJS升级为ESM后,配置了Node选项--experimental-specifier-resolution=node --loader ts-node/esm,并在package.json中设置"type": "module",正常运行无异常,但当步骤定义文件存在TypeScript错误(比如访问接口不存在的属性)时,调用runCucumber会触发ERR_UNHANDLED_REJECTION;添加catch后仅得到{ diagnosticCodes: [ 2353 ] },无法看到具体错误位置和描述。查看CucumberJS源码发现,问题出在文件加载逻辑await import((0, node_url_1.pathToFileURL)(path).toString());,加载失败时未传递详细错误信息。


相关配置与代码

调用Cucumber的代码

const config: ILoadConfigurationOptions = {
  file: 'cucumber.mjs',
}
const { runConfiguration } = await loadConfiguration(config)
const run = await runCucumber(runConfiguration).catch(console.error)

tsconfig.json配置

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "module": "ES2022",
    "target": "ES6",
    "moduleResolution": "Node",
    "allowSyntheticDefaultImports": true,
    "alwaysStrict": true
  }
}

测试运行命令

yarn component:gen-org-tests && node --experimental-specifier-resolution=node --loader ts-node/esm --no-warnings index.ts

解决方案

方案1:强制ts-node输出完整错误信息

通过环境变量让ts-node在编译失败时打印详细诊断内容,修改运行命令:

yarn component:gen-org-tests && TS_NODE_LOG_ERRORS=true node --experimental-specifier-resolution=node --loader ts-node/esm --no-warnings index.ts

也可以在代码中提前设置环境变量:

process.env.TS_NODE_LOG_ERRORS = 'true';

// 后续Cucumber调用逻辑不变
const config: ILoadConfigurationOptions = {
  file: 'cucumber.mjs',
}
const { runConfiguration } = await loadConfiguration(config)
const run = await runCucumber(runConfiguration).catch(console.error)

方案2:自定义错误捕获逻辑

在catch中手动解析TypeScript诊断信息,补充详细错误描述:

const config: ILoadConfigurationOptions = {
  file: 'cucumber.mjs',
}
const { runConfiguration } = await loadConfiguration(config)
const run = await runCucumber(runConfiguration).catch(err => {
  if (err.diagnosticCodes) {
    // 引入TypeScript API获取详细诊断
    const ts = require('typescript');
    const program = ts.createProgram(['./steps/**/*.ts'], require('./tsconfig.json').compilerOptions);
    const diagnostics = ts.getPreEmitDiagnostics(program);
    
    console.error('TypeScript错误详情:');
    diagnostics.forEach(d => {
      const pos = program.getSourceFile(d.file!.fileName)!.getLineAndCharacterOfPosition(d.start!);
      console.error(`[${d.file!.fileName}:${pos.line+1}:${pos.character+1}] ${ts.flattenDiagnosticMessageText(d.messageText, '\n')}`);
    });
  }
  console.error('原始错误:', err);
});

方案3:提前编译TypeScript文件

跳过ts-node即时编译,先编译所有步骤定义文件再运行测试,确保错误在编译阶段就被暴露:

  1. 修改tsconfig.json,添加输出目录配置:
{
  "compilerOptions": {
    "experimentalDecorators": true,
    "module": "ES2022",
    "target": "ES6",
    "moduleResolution": "Node",
    "allowSyntheticDefaultImports": true,
    "alwaysStrict": true,
    "outDir": "./dist"
  },
  "include": ["./steps/**/*.ts", "./index.ts"]
}
  1. 调整运行命令:
yarn component:gen-org-tests && tsc && node --experimental-specifier-resolution=node --no-warnings dist/index.js

内容的提问来源于stack exchange,提问作者Cjmarkham

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 07:55:28