In my stencil component I am using @Watch to perform validation as below
@Watch('name')
validateName(newValue: string) {
const isBlank = typeof newValue !== 'string' || newValue === '';
console.log('AYE!', isBlank);
if (isBlank) { throw new Error('name: required') };
}
Now I would like to test this in jest
it('throws error when name is not supplied', async () => {
const { root } = await newSpecPage({ components: [MyComponent], html: '<my-component></my-component>' });
expect(() => {
root.name = '';
}).toThrow(new Error('name: required'))
})
And the result I'm getting is as below
expect(received).toThrow(expected)
Expected message: "name: required"
Received function did not throw
47 | expect(() => {
48 | root.name = '';
> 49 | }).toThrow(new Error('name: required'))
| ^
50 | })
51 | });
52 |
console.log
AYE! true
at MyComponent.validateName (src/xxx/my-component.tsx:31:13)
at Array.map (<anonymous>)
I'm wondering how should I catch the error threw from validateName?