-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: prevent window.onerror from being overriden
- Loading branch information
Showing
2 changed files
with
93 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,68 @@ | ||
import ErrorPlugin from 'src/plugins/error'; | ||
|
||
beforeAll(() => { | ||
window.onerror = function () { | ||
// placeholder | ||
}; | ||
new ErrorPlugin().onCreated(); | ||
}); | ||
|
||
const errorOcupied = jest.fn(); | ||
jest.spyOn(ErrorPlugin, 'sendMessage').mockImplementation(errorOcupied); | ||
const errorOccupied = jest.fn(); | ||
jest.spyOn(ErrorPlugin, 'sendMessage').mockImplementation(errorOccupied); | ||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
describe('Error plugin', () => { | ||
it('window.onerror', () => { | ||
expect(window.onerror).not.toBe(null); | ||
window.onerror!('', '', 0, 0, new Error('throw error')); | ||
expect(errorOcupied).toHaveBeenCalledTimes(1); | ||
describe('Uncaught Error', () => { | ||
it('Have initiator value', (done) => { | ||
expect(window.onerror).not.toBeFalsy(); | ||
|
||
setTimeout(() => { | ||
throw new Error('Unit test'); | ||
}); | ||
setTimeout(() => { | ||
expect(errorOccupied).toHaveBeenCalledTimes(1); | ||
done(); | ||
}, 10); | ||
}); | ||
it('Register new error function', (done) => { | ||
const fn = jest.fn(); | ||
window.onerror = fn; | ||
setTimeout(() => { | ||
throw new Error('Unit test'); | ||
}); | ||
|
||
setTimeout(() => { | ||
expect(fn).toHaveBeenCalledTimes(1); | ||
expect(errorOccupied).toHaveBeenCalledTimes(1); | ||
done(); | ||
}, 10); | ||
}); | ||
it('Only the last registration will take effect if assign many times', (done) => { | ||
const fn1 = jest.fn(); | ||
const fn2 = jest.fn(); | ||
const fn3 = jest.fn(); | ||
window.onerror = fn1; | ||
window.onerror = fn2; | ||
window.onerror = fn3; | ||
|
||
setTimeout(() => { | ||
throw new Error('Unit test'); | ||
}); | ||
setTimeout(() => { | ||
expect([ | ||
fn1.mock.calls.length, | ||
fn2.mock.calls.length, | ||
fn3.mock.calls.length, | ||
]).toEqual([0, 0, 1]); | ||
done(); | ||
}, 10); | ||
}); | ||
}); | ||
it('window.addEventListener("error", fn)', () => { | ||
it('Resource load failed error', () => { | ||
window.dispatchEvent(new Event('error')); | ||
expect(errorOcupied).toHaveBeenCalledTimes(1); | ||
expect(errorOccupied).toHaveBeenCalledTimes(1); | ||
}); | ||
it('Promise unhandledrejection', () => { | ||
it('Unhandledrejection error', () => { | ||
window.dispatchEvent(new Event('unhandledrejection')); | ||
expect(errorOcupied).toHaveBeenCalledTimes(1); | ||
expect(errorOccupied).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |