Files
Yoni Goldberg 6b8c69014d Added nock
2023-05-08 13:25:37 +03:00

1.2 KiB
Raw Permalink Blame History

Specify a port in production, randomize in testing



One Paragraph Explainer

When writing component/integration tests, the web server should be started by the tests in the same process - this opens the door for many desirable testing features like mocking, coverage, and more. In a multi-process test runner, multiple web server instances will be opened. If these instances try to open the same port, they will collide. In testing only, let the server randomize a port to prevent collisions. This can easily achieved by providing an ephemeral port, the number zero, so the operating system will allocate an available port



Code Example starting the web server with testing in-mind

// api-under-test.js
const initializeWebServer = async () => {
  return new Promise((resolve, reject) => {
    // Fixed port in production, a zero port (ephemeral) for testing
    const webServerPort = process.env.PORT ? process.env.PORT : 0;
    expressApp = express();
    connection = expressApp.listen(webServerPort, () => {
      // No port
      resolve(expressApp);
    });
  });
};

// test.js
beforeAll(async () => {
  expressApp = await initializeWebServer(); // No port
});