This solution is generated by ChatGPT, correctness is not guaranteed.
AI solution for JavaScript Coding Question on BFE.dev
161. toBe() or not.toBe()
Here's a possible implementation of myExpect
:
function myExpect(value) {
return {
toBe(expected) {
if (value === expected) {
console.log("✅ Test passed");
} else {
console.log(`❌ Test failed: expected ${expected}, but got ${value}`);
}
},
not: {
toBe(expected) {
if (value !== expected) {
console.log("✅ Test passed");
} else {
console.log(`❌ Test failed: expected ${value} not to be ${expected}`);
}
},
},
};
}
You can use it in your tests as follows:
myExpect(3).toBe(3); // ✅ Test passed
myExpect(4).toBe(3); // ❌ Test failed: expected 3, but got 4
myExpect(3).not.toBe(3); // ❌ Test failed: expected 3 not to be 3
myExpect(4).not.toBe(3); // ✅ Test passed
Note that this is just one possible implementation, and there are many ways to achieve the same result.