How to resolve "TypeError: Cannot read property 'equal' of undefined" in Mocha
How to resolve "TypeError: Cannot read property 'equal' of undefined" in Mocha
当我运行以下两个代码文件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));
});
});
测试 1 通过,但对于测试 2,出现以下错误:
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)
请问我应该如何解决这个错误并通过第二次测试?
谢谢。
expect(count("a").to.equal(1));
应该是 expect(count("a")).to.equal(1);
。括号位置错误。
当我运行以下两个代码文件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));
});
});
测试 1 通过,但对于测试 2,出现以下错误:
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)
请问我应该如何解决这个错误并通过第二次测试?
谢谢。
expect(count("a").to.equal(1));
应该是 expect(count("a")).to.equal(1);
。括号位置错误。