Let's say we have a code like below that runs in node.
module.exports.registerUser = (username) => {
if (!username) throw new Error('Username is required.');
return { id: new Date().getTime(), username: username }
}
In the code, the scenario where Error should be thrown are when the username
is invalid.
For the username
to be invalid, the username
should be one of these: False
, 0
, null
, ''
, undefined
.
In the jest documentation, there is a section about parametrized (data-driven) tests in jest. However it is not clear how to handle erroneous situations in a bulk.
The code that I've come up is as below. Hope it helps!
describe("registerUser", () => {
it.each([null, undefined, NaN, "", 0, false])(
"should throw if username is falsy",
(inputs) => {
expect(() => lib.registerUser(input)).toThrow();
}
);
});
Top comments (0)