This matcher allows you to assert that a single attribute of the structure is invalid, optionally passing the array of error messages for that attribute:
constUser=attributes({ name: { type: String, required:true }, age: { type: Number, required:true },})(classUser {});constuser=newUser({ age:42 });// passes, because name is invalidexpect(user).toHaveInvalidAttribute(['name']);// fails, because age is validexpect(user).toHaveInvalidAttribute(['age']);// passes, because name is invalid with this messageexpect(user).toHaveInvalidAttribute(['name'], ['"name" is required']);// fails, because name is invalid but not with this messageexpect(user).toHaveInvalidAttribute(['name'], ['"name" is not cool']);// passes. Notice that you can even use arrayContaining to check for a subset of the errrosexpect(user).toHaveInvalidAttribute(['name'],expect.arrayContaining(['"name" is required']));// passes. And stringContaining can be used as wellexpect(user).toHaveInvalidAttribute(['name'], [expect.stringContaining('required')]);
toHaveInvalidAttributes([ { path, messages } ])
This matcher allows you to assert that multiple attributes of the structure are invalid, optionally passing the array of error messages for each attribute:
constUser=attributes({ name: { type: String, required:true }, age: { type: Number, required:true },})(classUser {});constuser=newUser({ age:42 });// passes, because name is invalidexpect(user).toHaveInvalidAttributes([{ path: ['name'] }]);// fails, because age is validexpect(user).toHaveInvalidAttributes([{ path: ['age'] }]);// fails, because name is invalid but age is validexpect(user).toHaveInvalidAttributes([{ path: ['name'] }, { path: ['age'] }]);// passes, because name is invalid with this messageexpect(user).toHaveInvalidAttributes([{ path: ['name'], messages: ['"name" is required'] }]);// fails, because name is invalid but not with this messageexpect(user).toHaveInvalidAttributes([{ path: ['name'], messages: ['"name" is not cool'] }]);// passes. Notice that you can even use arrayContaining to check for a subset of the errrosexpect(user).toHaveInvalidAttributes([ { path: ['name'], messages:expect.arrayContaining(['"name" is required']) },]);// passes. And stringContaining can be used as wellexpect(user).toHaveInvalidAttributes([ { path: ['name'], messages: [expect.stringContaining('required')] },]);