Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/danielschaffer/ts-custom-error-shim
A TypeScript custom error shim that allows properly testing custom errors
https://github.com/danielschaffer/ts-custom-error-shim
custom-error testing typescript unit-testing
Last synced: about 2 months ago
JSON representation
A TypeScript custom error shim that allows properly testing custom errors
- Host: GitHub
- URL: https://github.com/danielschaffer/ts-custom-error-shim
- Owner: DanielSchaffer
- License: mit
- Created: 2018-03-02T20:41:21.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-04-23T17:02:56.000Z (almost 2 years ago)
- Last Synced: 2024-11-30T07:39:15.726Z (2 months ago)
- Topics: custom-error, testing, typescript, unit-testing
- Language: TypeScript
- Homepage:
- Size: 163 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ts-custom-error
Extending `Error` does not work completely correctly with TypeScript
[starting with v2.1](https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work).While there isn't a completely function way of fixing this, this utility
seeks at least make unit testing custom errors a little easier. It works
by completely replacing the `Error` object on the global scope. This of
course means that using this on your actual build is likely a terrible
idea, but it will help you test your custom errors.Note that this is not required for working with plain ES code, since
it works correctly with Node (v8) and most browsers. The problem is
specific to TypeScript.# Installation
Install from NPM:
```bash
npm install ts-custom-error
```# Usage
All that is needed for use is to import the package at the beginning of
your tests (before your actual code is imported). Since it replaces the
`Error` class, you do not need to change any of your custom errors to
subclass anything other than `Error`.**Mocha:**
```bash
mocha --require ts-custom-error src/**/*.spec.ts
```**Others:**
```typescript
import 'ts-custom-error';
```