Skip to content

Commit

Permalink
Process handler test (#76)
Browse files Browse the repository at this point in the history
* added test for dwn-server

Signed-off-by: captain-Akshay <[email protected]>

* added test for process handler

Signed-off-by: captain-Akshay <[email protected]>

---------

Signed-off-by: captain-Akshay <[email protected]>
  • Loading branch information
captain-Akshay authored Oct 18, 2023
1 parent fef5b82 commit eb67a75
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 2 deletions.
123 changes: 122 additions & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@
"eslint-plugin-todo-plz": "^1.3.0",
"husky": "^8.0.0",
"lint-staged": "^14.0.1",
"mocha": "10.2.0",
"mocha": "^10.2.0",
"prettier": "3.0.3",
"sinon": "^16.1.0",
"supertest": "6.3.3",
"typescript": "^5.1.6"
},
Expand Down
53 changes: 53 additions & 0 deletions tests/process-handler.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { expect } from 'chai';
import sinon from 'sinon';

import { config } from '../src/config.js';
import { DwnServer } from '../src/dwn-server.js';
import { clear, dwn } from './test-dwn.js';

describe('Process Handlers', function () {
let dwnServer: DwnServer;
const options = {
dwn: dwn,
config: config,
};
let processExitStub: sinon.SinonStub;

before(async function () {
dwnServer = new DwnServer(options);
});
beforeEach(async function () {
await dwnServer.start();
processExitStub = sinon.stub(process, 'exit');
});
afterEach(async function () {
process.removeAllListeners('SIGINT');
process.removeAllListeners('SIGTERM');
process.removeAllListeners('uncaughtException');
await clear();
processExitStub.restore();
});
it('should stop when SIGINT is emitted', async function () {
process.emit('SIGINT');
expect(dwnServer.httpServer.listening).to.be.false;
expect(processExitStub.called).to.be.false; // Ensure process.exit is not called
});

it('should stop when SIGTERM is emitted', async function () {
process.emit('SIGTERM');
expect(dwnServer.httpServer.listening).to.be.false;
expect(processExitStub.called).to.be.false; // Ensure process.exit is not called
});

it('should log an error for an uncaught exception', function () {
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
expect(consoleErrorStub.calledOnce).to.be.true;

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

0 comments on commit eb67a75

Please sign in to comment.