One of the challenges I'm facing involves a simple function that creates a string from a complex object. To illustrate, consider the following implementation:
public generateMessage(property: string): string {
return `${property} more text.`;
}
In my current testing setup, I have the following test:
it('starts the message with the property name', () => {
const property = 'field';
const message: string = myClass.generateMessage(property);
expect(message).toEqual(`${property} more text.`);
});
The key criterion in this scenario is ensuring that the generated message begins with the specified property. Is there a way to validate if a string indeed starts with the designated property? Conceptually, it could be achieved using the following pseudo code:
expect(message).toStartWith(property);
Alternatively, do I need to create custom logic utilizing the startsWith()
method for strings? One feasible approach that comes to mind currently would entail:
expect(message.startsWith(property)).toBeTruthy();