Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
feat(make-promises-safe): treat unhandledRejection as uncaught except…
Browse files Browse the repository at this point in the history
…ion (#174)
  • Loading branch information
JAdshead authored Jun 10, 2020
1 parent 3a5ee9c commit 5fe0245
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 188 deletions.
41 changes: 14 additions & 27 deletions __tests__/server/utils/pollModuleMap.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,6 @@ jest.mock('../../../src/server/utils/stateConfig', () => ({
restoreModuleStateConfig: jest.fn(),
}));

async function waitForPromiseThatShouldThrow(p) {
const shouldThrowCheckError = new Error('should have thrown');
try {
await p;
throw shouldThrowCheckError;
} catch (err) {
if (err === shouldThrowCheckError) {
throw shouldThrowCheckError;
}
return err;
}
}

describe('pollModuleMap', () => {
jest.spyOn(console, 'log').mockImplementation(() => {});
jest.spyOn(console, 'warn').mockImplementation(() => {});
Expand Down Expand Up @@ -179,7 +166,7 @@ describe('pollModuleMap', () => {
// pollModuleMap run 1
.mockImplementationOnce(() => { throw new Error('STDOUT pipe closed unexpectedly'); });

await waitForPromiseThatShouldThrow(pollModuleMap());
await pollModuleMap();

expect(getModulesUsingExternals).toHaveBeenCalledTimes(1);
expect(setModulesUsingExternals).toHaveBeenCalledTimes(1);
Expand All @@ -195,7 +182,7 @@ describe('pollModuleMap', () => {
// pollModuleMap run 1
.mockImplementationOnce(() => { throw new Error('STDOUT pipe closed unexpectedly'); });

await waitForPromiseThatShouldThrow(pollModuleMap());
await pollModuleMap();
expect(backupModuleStateConfig).toHaveBeenCalled();
expect(restoreModuleStateConfig.mock.calls).toMatchSnapshot();
});
Expand All @@ -205,7 +192,7 @@ describe('pollModuleMap', () => {

getModulesUsingExternals.mockImplementationOnce(() => { throw new Error('failed to get modules using externals'); });

await waitForPromiseThatShouldThrow(pollModuleMap());
await pollModuleMap();
expect(getModulesUsingExternals).toHaveBeenCalledTimes(1);
expect(setModulesUsingExternals).not.toHaveBeenCalled();
});
Expand All @@ -217,7 +204,7 @@ describe('pollModuleMap', () => {
.mockImplementationOnce(() => { /* noop a few times */ })
// pollModuleMap run 1
.mockImplementationOnce(() => { throw new Error('STDOUT pipe closed unexpectedly'); });
await waitForPromiseThatShouldThrow(pollModuleMap());
await pollModuleMap();

expect(setTimeout).toHaveBeenCalledTimes(1);
expect(setTimeout.mock.calls[0][0]).toBe(pollModuleMap);
Expand Down Expand Up @@ -245,7 +232,7 @@ describe('pollModuleMap', () => {
expect(setTimeout).toHaveBeenCalledTimes(2);
expect(setTimeout.mock.calls[1][0]).toBe(pollModuleMap);

await waitForPromiseThatShouldThrow(pollModuleMap());
await pollModuleMap();
expect(setTimeout).toHaveBeenCalledTimes(3);
expect(setTimeout.mock.calls[2][0]).toBe(pollModuleMap);
expect(setTimeout.mock.calls[2][1]).toBe(MIN_POLL_TIME);
Expand All @@ -266,7 +253,7 @@ describe('pollModuleMap', () => {
expect(setTimeout).toHaveBeenCalledTimes(2);
expect(setTimeout.mock.calls[1][0]).toBe(pollModuleMap);

await waitForPromiseThatShouldThrow(pollModuleMap());
await pollModuleMap();
expect(setTimeout).toHaveBeenCalledTimes(3);
expect(setTimeout.mock.calls[2][0]).toBe(pollModuleMap);
expect(setTimeout.mock.calls[2][1]).toBe(MIN_POLL_TIME);
Expand All @@ -287,7 +274,7 @@ describe('pollModuleMap', () => {
expect(setTimeout).toHaveBeenCalledTimes(2);
expect(setTimeout.mock.calls[1][0]).toBe(pollModuleMap);

await waitForPromiseThatShouldThrow(pollModuleMap());
await pollModuleMap();
expect(setTimeout).toHaveBeenCalledTimes(3);
expect(setTimeout.mock.calls[2][0]).toBe(pollModuleMap);
expect(setTimeout.mock.calls[2][1]).toBe(MIN_POLL_TIME);
Expand Down Expand Up @@ -382,7 +369,7 @@ describe('pollModuleMap', () => {
const { default: pollModuleMap } = load();
loadModulesPromise = Promise.reject(new Error('sample test error'));
console.error.mockImplementationOnce(() => { throw new Error('STDERR pipe closed unexpectedly'); });
await waitForPromiseThatShouldThrow(pollModuleMap());
await pollModuleMap();

expect(setTimeout).toHaveBeenCalledTimes(1);
});
Expand All @@ -396,15 +383,15 @@ describe('pollModuleMap', () => {

loadModulesPromise = Promise.reject(new Error('sample test error'));

await waitForPromiseThatShouldThrow(pollModuleMap());
await pollModuleMap();
expect(setTimeout).toHaveBeenCalledTimes(1);
expect(setTimeout.mock.calls[0][0]).toBe(pollModuleMap);

await waitForPromiseThatShouldThrow(pollModuleMap());
await pollModuleMap();
expect(setTimeout).toHaveBeenCalledTimes(2);
expect(setTimeout.mock.calls[1][0]).toBe(pollModuleMap);

await waitForPromiseThatShouldThrow(pollModuleMap());
await pollModuleMap();
expect(setTimeout).toHaveBeenCalledTimes(3);
expect(setTimeout.mock.calls[2][0]).toBe(pollModuleMap);
});
Expand All @@ -418,15 +405,15 @@ describe('pollModuleMap', () => {

loadModulesPromise = Promise.reject(new Error('sample test error'));

await waitForPromiseThatShouldThrow(pollModuleMap());
await pollModuleMap();
expect(setTimeout).toHaveBeenCalledTimes(1);
expect(setTimeout.mock.calls[0][0]).toBe(pollModuleMap);

await waitForPromiseThatShouldThrow(pollModuleMap());
await pollModuleMap();
expect(setTimeout).toHaveBeenCalledTimes(2);
expect(setTimeout.mock.calls[1][0]).toBe(pollModuleMap);

await waitForPromiseThatShouldThrow(pollModuleMap());
await pollModuleMap();
expect(setTimeout).toHaveBeenCalledTimes(3);
expect(setTimeout.mock.calls[2][0]).toBe(pollModuleMap);
});
Expand Down
171 changes: 11 additions & 160 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"immutable": "^4.0.0-rc.12",
"ip": "^1.1.5",
"lean-intl": "^4.2.2",
"make-promises-safe": "^5.1.0",
"matcher": "^3.0.0",
"opossum": "^5.0.0",
"opossum-prometheus": "^0.1.0",
Expand Down
Loading

0 comments on commit 5fe0245

Please sign in to comment.