Skip to content

Commit

Permalink
Better approach to testing process handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
thehenrytsai committed Jul 5, 2024
1 parent 9ea81e4 commit 80ee227
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/process-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export const unsetProcessHandlers = (handlers: ProcessHandlers): void => {
sigtermHandler
} = handlers;

process.off('unhandledRejection', unhandledRejectionHandler);
process.off('uncaughtException', uncaughtExceptionHandler);
process.off('SIGINT', sigintHandler);
process.off('SIGTERM', sigtermHandler);
process.removeListener('unhandledRejection', unhandledRejectionHandler);
process.removeListener('uncaughtException', uncaughtExceptionHandler);
process.removeListener('SIGINT', sigintHandler);
process.removeListener('SIGTERM', sigtermHandler);
};
32 changes: 24 additions & 8 deletions tests/process-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,25 @@ import { expect } from 'chai';
import sinon from 'sinon';

import { config } from '../src/config.js';
import type { Dwn } from '@tbd54566975/dwn-sdk-js';
import { DwnServer } from '../src/dwn-server.js';
import { getTestDwn } from './test-dwn.js';
import { Poller } from './poller.js';

describe('Process Handlers', function () {
let dwn: Dwn;
let dwnServer: DwnServer;
let processExitStub: sinon.SinonStub;

beforeEach(async function () {
const testDwn = await getTestDwn();
dwnServer = new DwnServer({ dwn: testDwn, config: config });
const dwn = await getTestDwn();
dwnServer = new DwnServer({ dwn, config: config });
await dwnServer.start();
processExitStub = sinon.stub(process, 'exit');
});

afterEach(async () => {
await dwnServer.stop();
console.log('server stop in Process Handlers tests');

process.removeAllListeners('SIGINT');
process.removeAllListeners('SIGTERM');
process.removeAllListeners('uncaughtException');
processExitStub.restore();
});

Expand All @@ -45,16 +42,35 @@ describe('Process Handlers', function () {
});
});

it('should log an error for an uncaught exception', function () {
it('should log an error for an uncaught exception', async () => {

// IMPORTANT: this test is a bit tricky to write because
// existing process `uncaughtException` listener/handler will result will trigger an error when we force an `uncaughtException` event
// causing the test to fail. So we need to remove the existing listener and add them back after the test.
// To be in full control of the test, we also create the DWN server (which adds it's own `uncaughtException` listener)
// AFTER removing the existing listener.
await dwnServer.stop();

// storing then removing existing listeners and adding back at the very end of the test
const existingUncaughtExceptionListeners = [...process.listeners('uncaughtException')];
process.removeAllListeners('uncaughtException');

dwnServer = new DwnServer({ dwn, config: config });
await dwnServer.start();

const consoleErrorStub = sinon.stub(console, 'error'); // Stub console.error
const errorMessage = 'Test uncaught exception';
const error = new Error(errorMessage);
process.emit('uncaughtException', error);

// Ensure console.error was called with the expected error message
console.log('console.error call count', consoleErrorStub.callCount);
expect(consoleErrorStub.calledOnce).to.be.true;

// Restore the original console.error
consoleErrorStub.restore();

// add back original listeners
existingUncaughtExceptionListeners.forEach(listener => process.on('uncaughtException', listener));
});
});
4 changes: 0 additions & 4 deletions tests/scenarios/registration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ if (!globalThis.crypto) {
globalThis.crypto = webcrypto;
}

console.log = (): void => {};
console.error = (): void => {};
console.info = (): void => {};

describe('Registration scenarios', function () {
const dwnMessageEndpoint = 'http://localhost:3000';
const termsOfUseEndpoint = 'http://localhost:3000/registration/terms-of-service';
Expand Down

0 comments on commit 80ee227

Please sign in to comment.