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
?
After changing a prop, you'll have to use the async
waitForChanges()
. You can check whether it throws by calling it and then usingrejects.toEqual(...)
. Also you'll have to return theexpect(...)
statement, otherwise your test will pass before the async code has finished (and you'd get aUnhandledPromiseRejectionWarning
in your console).