npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ember-test-friendly-error-handler

v3.0.0

Published

Build testable error handlers that don't throw in production...

Downloads

511

Readme

ember-test-friendly-error-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-error-handler

Usage

In your application code you would import the error handler generator, and invoke it with a descriptive label and your callback.

Ember.onerror

Ember.onerror is a hook that is invoked when an error is thrown by any code within the Ember run loop (e.g. {{action}}'s, component event methods, model hooks, etc). In practice, this is nearly all of your application code. Ember.onerror has the ability to "swallow" errors by handling them without rethrowing, and ultimately making the failure scenario impossible to detect while testing.

It is common for applications to leverage Ember.onerror to do error reporting and attempt to gracefully handle errors thrown within the application, and when possible prevent those errors from bubbling out and causing issues with the running application (or providing more detailed information when they do impact the app).

Without something like ember-test-friendly-error-handler, applications that implement Ember.onerror either have to replicate this addon's behavior, or are unable to properly test both the "production" mode (eg error swallowing) and development/testing mode (eg re-throw errors to make them possible to track down and fix).

Here is how an application might set this up:

// app/app.js
import Ember from 'ember';
import buildErrorHandler from 'ember-test-friendly-error-handler';

Ember.onerror = buildErrorHandler('Ember.onerror', (reason) => {
  reportErrorToService(reason);
  // whatever else you might want here...
});
// ...existing `app/app.js` content goes here...

Promises

To generate a promise rejection handler (aka .catch handler) you might do something like:

import buildErrorHandler from 'ember-test-friendly-error-handler';

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

Testing

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-error-handler provides you so that your test more closely resembles your production environment.

A test that does this might look like:

import { module, test } from 'qunit';
import { 
  squelchErrorHandlerFor,
  unsquelchAllErrorHandlers
} from 'ember-test-friendly-error-handler';

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

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

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

API

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

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

// the following are only present when testing
export function squelchErrorHandlerFor(label: string): void;
export function unsquelchAllErrorHandlers(): void;

Contributing

Installation

  • git clone <repository-url> this repository
  • cd ember-test-friendly-error-handler
  • npm install

Running

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/.