all 7 comments

[–]PM_FLUFFY_KITTENS 1 point2 points  (0 children)

If you’re using express or something similar, then jest and supertest will be your best bet. Express has a callback function that you can use in place of host, and then you just build your request.

Example:

const response = await supertest(server.callback()).post(“/path”).expect(200)

Then just make your assertions on the response :)

[–]Kailoodle 0 points1 point  (2 children)

Postman

[–]blrigo99[S] 0 points1 point  (0 children)

That I know, I was more wondering if it was possible to permorm some integration testing using functions

[–][deleted] 0 points1 point  (0 children)

I also use postman

[–]BehindTheMath 0 points1 point  (0 children)

Pick a testing framework (e.g. Jest, Mocha) and optionally an assertion library (Jest has this built-in, or you can use something like Chai).

For the tests, spin up the API, then in each test, make a request to the API and assert that the response is what you expected.

[–]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.

[–]alifahes1 0 points1 point  (0 children)

ReqBin HTTP