Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Testing Global Errors in Karma #2797

Open
ilyaigpetrov opened this issue Aug 3, 2017 · 0 comments
Open

Feature: Testing Global Errors in Karma #2797

ilyaigpetrov opened this issue Aug 3, 2017 · 0 comments

Comments

@ilyaigpetrov
Copy link

Setup

I need to test such function:

const testMe = () =>
  setTimeout(
    () => { throw new EvalError('foo'); },
    0,
);

Proposed Solution

it('throws error', (done) => {

  expect(testMe).to.eventually.throw.global(EvalError).then((thrown) => {

    expect(thrown.message).to.equal('foo');
    done();

  });

});

Current Solution

const catchGlobal = (errHandler) => {

  const originalOnError = window.onerror;
  window.onerror = () => {};
  window.addEventListener('error', (errEvent) => {

    window.onerror = originalOnError;
    setTimeout(() => errHandler(errEvent.error), 0);

  }, {
    capture: true,
    once: true,
  });

};

it('throws error', (done) => {

    catchGlobal((thrown) => {

      expect(thrown.message).to.be.equal('foo');
      done();

    });

});

Gory Details

  1. In API of Chrome web-extensions you may pass callbacks.
  2. If error is thrown in such callback, you can't catch it, except one case: error is thrown in setTimeout.
  3. So you wrap all callbacks in setTimeout and set global error catchers. I wrote a few functions to automate this process, which I need to be tested.
  4. For more details see: https://crbug.com/357568
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant