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?
DAH
Deno AI Helper57d 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
DAH
Deno AI Helper57d 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. 🦕
K
kewp57d 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!
More Posts
Debugging high (string) heap usageI was observing relatively high memory usage in my deno applications (web-applications). I haven't aVS Code integration is brokenHi all. Just opened my Deno project in VS Code and got: `Cannot set workspace settings: invalid typfollow up action after respond to the requestCurrently, i have this for my http interaction discord bot ```ts // ping.js execute(api: API, interaHow to apply @deno-types on a dynamic import```ts // @deno-types="npm:@types/react-dom@18/client" const { hydrateRoot: hydrate } = (await impCache fresh deps on docker problemI have a brand new fresh project with only the default deps and when i try to put my app in a dockerDeno fmt with verbatimModuleSyntax?Currently Deno fmt sorts *verbatim imports* alphabetically, this is conflicting with the *Organize IHow to use private remote modules hosted on GitHub on Deno Deploy?According to [Private Modules and Repositories](https://docs.deno.com/runtime/manual/basics/modules/Encryption using Deno KV and OAuth?I'm creating a note-taking app using most of Deno's tools. I was thinking about encrypting user noteDeno is not seeing a projectIt seems deno is not seeing a project i am using. Project link: https://github.com/Swifter1243/ReMa`arch` and `platform` are empty strings in `node:process`👋 hello! I'm trying to use a node project that uses `arch` and `platform` inside `node:process`, whNPM imports fail on JSON requireWhen importing modules via `npm:` protocol, the embedded esbuild will throw the following error if tuse-case: web component development in denoMy use-case is quite simple, really. But I have a hard time finding the tooling to work with it. - Starting project errorsI have error in url of DenoConditional callback with deno_kv_oauthHello I am using deno_kv_oauth for my application and am trying to do the following: I want users t