I have worked on mocha. One on my variable returns :
let datestring = new Date().toLocaleString();
Here my tested object :
const generateMessage = (from, text) => {
return {
from,
text,
datestring
}
}
here the test :
const expect = require("expect");
let {generateMessage} = require("./message")
describe("generateMessage", () => {
it("should generate correct message object", () => {
let from="Jen";
let text= "Some message";
let message = generateMessage(from, text);
console.log(message)
expect(message.datestring).toBe("string")
expect(message).toInclude({
from,
text
});
});
});
returns :
generateMessage { from: 'Jen', text: 'Some message', datestring: '7/21/2018, 9:57:31 PM' } 1) should generate correct message object
0 passing (27ms) 1 failing
1) generateMessage should generate correct message object: Error: expect(received).toBe(expected) // Object.is equality
Expected: "string"
Received: "7/21/2018, 9:57:31 PM" at Context.it (server/unit/message.test.js:13:36)
How it is possible the test fails since it returns effectively a string ?
Thanks
toBe
is used to check the value of not to check the type. Your test got error because the received value is "7/21/2018, 9:57:31 PM" but you expect to get the value "string". – deerawan