-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(factory): add static .throw factory to helpful errors for convin…
…ience
- Loading branch information
1 parent
15853e2
commit 3f82ef6
Showing
5 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,30 @@ | ||
import { HelpfulError } from './HelpfulError'; | ||
import { getError } from './getError'; | ||
|
||
describe('HelpfulError', () => { | ||
it('should produce a helpful, observable error message', () => { | ||
const error = new HelpfulError('the dogs were let out', { | ||
who: 'your mom', | ||
who: 'your mom', // 🙄😂 | ||
}); | ||
expect(error).toMatchSnapshot(); | ||
}); | ||
it('should be throwable in a ternary conveniently and precisely', () => { | ||
const error = getError(() => { | ||
// this case should not throw | ||
const customerOne: { phone: string | null } = { | ||
phone: 'yes', | ||
}; | ||
const phoneOne = | ||
customerOne.phone ?? HelpfulError.throw('phone one not found!'); | ||
|
||
// but this case should throw | ||
const customerTwo: { phone: string | null } = { | ||
phone: null, | ||
}; | ||
const phoneTwo = | ||
customerTwo.phone ?? HelpfulError.throw('phone two not found!'); | ||
}); | ||
expect(error).toBeInstanceOf(HelpfulError); | ||
expect(error.message).toEqual('phone two not found!'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters