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

执行npm test后VS Code终端未显示Jest测试结果求助

问题解决方案

一、npm test无测试结果输出的修复步骤

  • 指定测试文件路径:修改package.json的scripts字段,显式指定测试文件,避免Jest匹配规则问题:
    {"devDependencies": {"jest": "^29.5.0"},"scripts": {"test": "jest node.test.js"}}
    
    重新执行npm test查看结果。
  • 重新安装依赖:删除node_modules和package-lock.json,执行npm install后再运行测试,排除依赖损坏问题。
  • 验证Node版本兼容性:Jest 29.x要求Node.js >=14.15.0,执行node -v检查版本,过低则升级Node.js。
  • 查看详细日志:执行npm test -- --verbose或npx jest --watchAll --verbose,获取详细执行日志定位隐藏错误。

二、VS Code执行测试自动打开node.js的解决方法

  • 打开VS Code设置,搜索Testing: Open Test,将选项设置为Never,禁用自动跳转源文件的行为。
  • 右键点击测试用例选择Run Test,而非通过终端执行,也可避免该问题。

代码正确性验证

你的Node工厂函数和测试用例逻辑无误:

  • node.js代码:
    function Node(data) {
     let value = data || null;
     let next = null;
    
     return {
       value,
       next,
      };
    } 
    module.exports = Node;
    
  • node.test.js测试用例:
    const Node = require('./node');
    
    test('does Node create a new node with correct value ?', () => {
    let newNode = Node('Rabten');
    
    expect(newNode).toEqual({value: "Rabten", next: null})
    });
    
    导出与导入路径匹配,逻辑正确,排除代码本身问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 03:12:48