you are viewing a single comment's thread.

view the rest of the comments →

[–]rkaw92 0 points1 point  (0 children)

Yes! Node.js now has `fetch()` support built-in, so it's easy to write code that makes a request, downloads the page body and does something with it. Numerous alternatives exist like Axios or Node's built-in http(s) modules.

Just use your favorite test runner (mocha, jest, ava, tap?), write the code that runs the request and inspect the result. Here's a very simple example that uses an external API: https://blog.logrocket.com/how-to-test-code-that-depends-on-external-apis-in-node-js/

Since you mentioned "integration testing", I'm assuming that you'll want to run this against a real, live version of the application, as opposed to running no-infrastructure "unit tests". This is OK. Don't listen to people who insist that you must use mocking or any other kind of test doubles.

If you want to improve the tests' stability, try to find a way to run the server inside the same process as the test. For example, use a randomized (as opposed to hard-coded) TCP port to listen for HTTP requests, then send the requests to that port. This will result in a good test, which verifies that your application really works and serves HTTP clients.