Javascript 与Chai相关的错误消息:";AssertionError:预期未定义为“完全相等”;

Javascript 与Chai相关的错误消息:";AssertionError:预期未定义为“完全相等”;,javascript,chai,Javascript,Chai,我编写了一个函数,当给定一个对象列表和 id,返回相同的列表,但相应的对象标记为活动 (所有其他对象不应处于活动状态) 返回: [ { id: 1, active: false }, { id: 2, active: false }, { id: 3, active: false }, { id: 4, active: true } ] 它就像一个魔咒,除了当我运行“npm run[filename]”时,我会收到一条错误消息: Running Tests for [filenam

我编写了一个函数,当给定一个对象列表和 id,返回相同的列表,但相应的对象标记为活动 (所有其他对象不应处于活动状态)

返回:

[ { id: 1, active: false },
  { id: 2, active: false },
  { id: 3, active: false },
  { id: 4, active: true } ]
它就像一个魔咒,除了当我运行“npm run[filename]”时,我会收到一条错误消息:

Running Tests for [filename].
------------
[ { id: 1, active: false },
  { id: 2, active: false },
  { id: 3, active: false },
  { id: 4, active: true } ]


  markActive
  1) Case 1 (Given Sample)
  2) Case 2 (String IDs)


  0 passing (16ms)
  2 failing

  1) markActive Case 1 (Given Sample):
     AssertionError: expected undefined to deeply equal [ { id: 1,        
     active: false },
     { id: 2, active: true },
     { id: 3, active: false },
     { id: 4, active: false } ]
     at Function.assert.deepEqual            
     (node_modules/chai/lib/chai/interface/assert.js:216:32)
      at Context.it (tests/test_02.js:23:12)

  2) markActive Case 2 (String IDs):
     AssertionError: expected undefined to deeply equal [ { id: '1',     
     active: false },
     { id: '2', active: true },
     { id: '3', active: false },
     { id: '4', active: false } ]
     at Function.assert.deepEqual   
     (node_modules/chai/lib/chai/interface/assert.js:216:32)
     at Context.it (tests/test_02.js:40:12)
知道我做错了什么吗?下面是设置测试的代码:

const chai    = require("chai");
const sinon   = require("sinon");
const assert  = chai.assert;

const markActive = require("../answers/02.js");

describe("markActive", () => {

  it("Case 1 (Given Sample)", () => {
  var list = [
         { id: 1, active: false },
         { id: 2, active: false },
         { id: 3, active: true  },
         { id: 4, active: false }
       ];
  var newList = markActive(list, 2);
  var targetList = [
         { id: 1, active: false },
         { id: 2, active: true  },
         { id: 3, active: false },
         { id: 4, active: false }
       ];
   assert.deepEqual(newList, targetList);
   });

  it("Case 2 (String IDs)", () => {
    var list = [
         { id: "1", active: false },
         { id: "2", active: false },
         { id: "3", active: true  },
         { id: "4", active: false }
       ];
    var newList = markActive(list, "2");
    var targetList = [
         { id: "1", active: false },
         { id: "2", active: true },
         { id: "3", active: false  },
         { id: "4", active: false }
       ];
    assert.deepEqual(newList, targetList);
  });

});

您的函数没有返回任何内容,因此您尝试设置为结果的任何变量都将设置为
未定义

要解决这个问题,只需在函数末尾添加一个
return
语句

function markActive(list, value) {
  list.forEach((id) => {
    if (id.active = (id.id === value)) {
      return true;
    } else {
      return false;
    }
  });

  return list; // return the updated list
}

注意:值得一提的是,由于数组是被引用的,所以您正在原地修改值。这就是为什么即使没有记录返回值,在函数外部定义的数组仍然有更新的结果。如果在同一个列表上多次运行
markActive()
函数,可能会产生意外的副作用。如果您希望返回新列表,请研究Javascript中复制和深度复制数组的方法。

请编辑您的问题并添加失败测试的代码。您是指我用来设置测试的代码吗?
function markActive(list, value) {
  list.forEach((id) => {
    if (id.active = (id.id === value)) {
      return true;
    } else {
      return false;
    }
  });

  return list; // return the updated list
}