I have a simple function that looks like this:
function foo({ platform }) {
if (platform === 'all') {
throw new Error('Platform value can only be android or ios');
}
return `${platform}`;
}
After writing unit tests, the results were not as expected. Here is the test code:
it('should correctly return the result based on platform', () => {
expect(foo({ platform: 'ios' })).toBe('ios');
expect(foo({ platform: 'android' })).toBe('android');
expect(foo({platform: 'all'})).toThrow(new Error('Platform value can only be android or ios'));
});
Unfortunately, the final test case failed without providing useful information:
FAIL src/utils/__test__/foo.test.ts
● foo() › should return correct result with platform
Platform value can only be android or ios
16 | }) {
17 | if (platform === 'all') {
> 18 | throw new Error('Platform value can only be android or ios');
| ^
19 | }
20 |
21 | return `${platform}`;
at xx (src/utils/foo.ts:18:11)
Even after wrapping the entire expect
statement in a try-catch block, the test continued to fail.