How to resolve "TypeError: Cannot read property 'equal' of undefined" in Mocha

Viewed 1594

When I run the following two files of code with mocha test/test.js:

// index.js
const count = (string) => {
  if (string === "") {
    return {};
  } else {
    return 1;
  }
};

module.exports = count;

// test/test.js
const { expect } = require("chai");
const count = require("../index");

describe("count characters in string", () => {
  it("should return empty object literal when string is empty", () => {
    expect(count("")).to.eql({});
  });

  it("returns an object with a count of 1 for a single character", () => {
    expect(count("a").to.equal(1));
  });
});

Test 1 passes, but for test 2 I get the following error:

  1) count characters in string
       returns an object with a count of 1 for a single character:
     TypeError: Cannot read property 'equal' of undefined
      at Context.<anonymous> (test/test.js:10:25)
      at processImmediate (internal/timers.js:456:21)

Please could you advise on what I should do to resolve this error and get my second test to pass?

Thank you.

1 Answers

expect(count("a").to.equal(1)); should be expect(count("a")).to.equal(1);. Wrong parenthesis placement.

Related