karma-runner / karma-runner/karma

Feature: Testing Global Errors in Karma

Open
#2,797 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
12k
Forks
1.7k
PR merge metrics
No merged PRs in 30d

Description

### Setup

I need to test such function:

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

### Proposed Solution
```js
it('throws error', (done) => {

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

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

});

});
```
### Current Solution
```js
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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.