In order to properly handle async code, you can use the following approaches:
- use the done callback
- return the promise(if any of course)
- use async/await: make sure the callback that you're passing to the it/test function is "asynced"
- in order to test async code which should fail, you must use try/catch or switch to the second appoach listed here and use the catch method on the promise that had been rejected;
- Arrange
- Action
- Assertion
describe("The validateName function", () => {
it("should create new error message if the user name is less than 5 characters", () => {
// Arrange
const user2 = new User("tom", "tom@test.com");
// Action
user2.validateName();
// Assertion
expect(user2.errors).toEqual(["the name must be at least 5 chars long"]);
});
});
If your test is passing, make sure to temporary change something on the passing test. If the test fails now, then the test is not evergreen.