kewp
kewp•5mo ago

Testing API server

I'm trying to setup an API server using deno and am trying to figure out what the canonical way of testing it might be. I'd like to run the server and then run tests against it - I'm assuming you do something like Deno.Command to start the server? I'd like a script I can run like test.sh that will spawn the server, run tests against it, and then watch for any changes between the two ... Any examples of this in the codebase / online?
3 Replies
Deno AI Helper
Deno AI Helper•5mo ago
To set up an API server and test it in Deno, you can indeed use Deno.run to start the server as a subprocess within your test script. The documentation provides guidance on how to use Deno's testing API, which supports a custom set of APIs to enable testing. These are built around providing information to enable testing but can also be used by other language server clients to provide a similar interface. Here's a general approach you can take to test your API server: 1. Write your API server code and ensure it's modular enough to be started and stopped programmatically. 2. Create a test script that uses Deno.run to start your server as a subprocess. 3. Write your tests using Deno's built-in testing functions, such as Deno.test. 4. In your test script, make HTTP requests to your server to test its functionality. 5. After the tests are complete, close the server subprocess. 6. Optionally, you can use the --watch flag with deno test to watch for file changes and rerun tests automatically. Here's a simplified example of what your test script might look like: ```typescript // test_server.ts import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; // Start the server as a subprocess const serverProcess = Deno.run({ cmd: ["deno", "run", "--allow-net", "your_server.ts"], stdout: "piped", stderr: "piped", }); // Give the server a moment to start up await new Promise((resolve) => setTimeout(resolve, 1000)); Deno.test("It should respond with 'Hello, World!'", async () => { const response = await fetch("http://localhost:8000/"); const body = await response.text(); assertEquals(body, "Hello, World!"); }); // Add more tests as needed... // After all tests are done, close the server
Deno AI Helper
Deno AI Helper•5mo ago
process serverProcess.close(); serverProcess.stdout.close(); serverProcess.stderr.close();
To run this script, you would use the following command:
To run this script, you would use the following command:
sh deno test --allow-net test_server.ts
If you want to watch for changes, you can add the `--watch` flag:
If you want to watch for changes, you can add the `--watch` flag:
sh deno test --allow-net --watch test_server.ts ``` Please note that this is a general guide and not a specific example from the documentation. You may need to adjust the code to fit the specifics of your server and tests. The documentation does not provide a direct example of a script that spawns a server, runs tests against it, and watches for changes, but you can use the above approach as a starting point and tailor it to your needs. 🦕
kewp
kewp•5mo ago
what if the server returns an error? how can i check that? i keep getting a connection refused error: deno test --allow-net --allow-run test_server.ts Check file:///Users/karl/fcache/test_server.ts starting server Listening on http://localhost:8000/ running 1 test from ./test_server.ts It should respond with 'Hello, World!' ... FAILED (13ms) ERRORS It should respond with 'Hello, World!' => ./test_server.ts:25:6 error: TypeError: error sending request for url (http://localhost:8000/): error trying to connect: tcp connect error: Connection refused (os error 61) const response = await fetch("http://localhost:8000/"); ^ at async mainFetch (ext:deno_fetch/26_fetch.js:170:12) at async fetch (ext:deno_fetch/26_fetch.js:391:7) at async file:///Users/karl/fcache/test_server.ts:26:20 FAILURES It should respond with 'Hello, World!' => ./test_server.ts:25:6 FAILED | 0 passed | 1 failed (14ms) error: Test failed when I wrap in Deno.test it fails: Deno.test("It should respond with 'Hello, World!'", async () => { const response = await fetch("http://localhost:8000/"); const body = await response.text(); assertEquals(body, "Hello, World!"); }); but when I just run it without it works! const response = await fetch("http://localhost:8000/"); const body = await response.text(); assertEquals(body, "Hello, World!"); The issue was that the process was closed outside of the test, which made the process close down before the test had even run!