TBD54566975 / dwn-server

DWN server - host anywhere publicly accessible for DIDs to use
Apache License 2.0
40 stars 41 forks source link

Changes to error handling and improvement to HTTP server test cases #28

Closed frankhinek closed 1 year ago

frankhinek commented 1 year ago

What's changed:

1. Changes to error handling:


2. Tests close HTTP server even in the event of an assertion failure

Previously, many of the http-api.ts test cases started an HTTP server:

const server = httpApi.listen(3000);

At the end of each of the tests (6 total), server instance methods were called to shutdown:

server.close();
server.closeAllConnections();

When an assertion fails, it throws an error, and the execution of the current function is stopped immediately. This means that if an assertion fails before the server.close() and server.closeAllConnections() calls, those calls are never reached and thus never executed. As a result, subsequent tests would report an EADDRINUSE error because the prior test's server was still listening on port 3000.

This PR modifies the tests to create a new server before each test starts using the beforeEach() hook. The execution of close() and closeAllConnections() is placed in the afterEach() hook, which is executed after each test, regardless of whether it passed or failed. This all reduces the duplicative code needed for each test.