Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process handler test #76

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});