# ember-test-friendly-catch-handler

> Build testable catch handlers that don't throw in production...

Latest version **1.0.0** (published 2017-09-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install ember-test-friendly-catch-handler
pnpm add ember-test-friendly-catch-handler
yarn add ember-test-friendly-catch-handler
bun add ember-test-friendly-catch-handler
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2017-09-30 |
| First published | 2017-09-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | ^4.5 \|\| 6.* \|\| >= 7.* |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | rwjblue |
| Keywords | ember-addon |

## Links

- npm: https://www.npmjs.com/package/ember-test-friendly-catch-handler
- npm.io page: https://npm.io/package/ember-test-friendly-catch-handler

## Dependencies (1)

- [ember-cli-babel](https://npm.io/package/ember-cli-babel.md) ^6.8.2

## Recent versions

- 1.0.0 (latest) — 2017-09-30

## README

# ember-test-friendly-catch-handler

In production, you often want to catch certain types of errors such as network errors (e.g. `myModel.save().catch(() => this.showNetworkFailureMessage())`) however these kinds of generic catch handlers can wreak havoc on your tests.  In tests, _most_ of the time you want these uncaught errors to _actually_ fail your tests unless explicitly testing the generic catch handler behaviors (e.g. `this.showNetworkFailureMessage`).

## Installation

    ember install ember-test-friendly-catch-handler

## Usage

In your application code you would import the catch generator, and invoke it with a descriptive label and your callback.
```js
import catchGenerator from 'ember-test-friendly-catch-handler';

// ... snip ...
myModel.save()
  .catch(catchGenerator('save-my-model', () => this.showNetworkFailureMessage()));
```

When you need to test the generic handler behavior (`this.showNetworkFailureMessage()` above), you need to disable the automatic error re-throwing behavior that `ember-test-friendly-catch-handler` provides you so that your test more closely resembles your production environment.

A test that does this might look like:

```js
import { module, test } from 'qunit';
import { 
  squelchCatchHandlerFor,
  unsquelchAllCatchHandlers
} from 'ember-test-friendly-catch-handler';

module('some good description', {
  afterEach() {
    unsquelchAllCatchHandlers(); 
  }
});

test('network failure message is displayed', function(assert) {
  squelchCatchHandlerFor('save-my-model');

  triggerNetworkFailure();         // ⚡️
  return triggerModelSave()
    .then(() => {
      assertNetworkFailureShown(); // 😼
    });
});
```

## API

The following interface describes the `ember-test-friendly-catch-handler` module's API:

```ts
export default function(label: string, callback: Function): Function;

// the following are only present when testing
export function squelchCatchHandlerFor(label: string): void;
export function unsquelchAllCatchHandlers(): void;
```

## Contributing

### Installation

* `git clone <repository-url>` this repository
* `cd ember-test-friendly-catch-handler`
* `npm install`


### Running

* `ember serve`
* Visit your app at [http://localhost:4200](http://localhost:4200).

### Running Tests

* `npm test` (Runs `ember try:each` to test your addon against multiple Ember versions)
* `ember test`
* `ember test --server`

### Building

* `ember build`

For more information on using ember-cli, visit [https://ember-cli.com/](https://ember-cli.com/).

---
_Source: https://npm.io/package/ember-test-friendly-catch-handler · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
