Testing examples? #194
Replies: 8 comments 8 replies
-
Hey guys. Yeah, would be cool to see at least one because I have no idea how to test my endpoints :) |
Beta Was this translation helpful? Give feedback.
-
Hi. I'm trying to figure out how to test the Nitro Server endpoints with something like Vitest or Jest. On previous Koa and Express projects I've used supertest to achieve this but can't figure out how to implement this in the Nitro version. Any help would be greatly appreciated. |
Beta Was this translation helpful? Give feedback.
-
You can find examples on how to test nitro directly in the tests https://github.com/unjs/nitro/blob/main/test/tests.ts :
|
Beta Was this translation helpful? Give feedback.
-
hey, guys, any update here? I'm new to Nitro, I'd like to see how to write tests for my Nitro code, like what people can do with pytest. |
Beta Was this translation helpful? Give feedback.
-
I found this repo It looks like we can run tests with this setup, but it still can't handle hot-reload or partial unit-test. I really hope some guide or helper module from the official. |
Beta Was this translation helpful? Give feedback.
-
I opened an issue for missing documentation but I wonder if there are even any utils for testing? This repo is a very good start for sure: https://github.com/johannschopplich/nitro-starter I opened an issue: #2307 |
Beta Was this translation helpful? Give feedback.
-
Hey, guys. Here I have prepared a simple example with testing. If you have any questions, I am ready to help you. |
Beta Was this translation helpful? Give feedback.
-
@MickL, @pi0 While we wait for #2303, I'm wondering what the best practices are for unit testing handlers. I'm particularly interested in mocking Given the following fixture: // server/api/echo/[value].post.ts
export default eventHandler(async (event) => {
const { context: { foo }, method, path } = event;
const headers = getHeaders(event);
const routerParams = getRouterParams(event);
const body = await readBody(event);
return {
method,
path,
routerParams,
headers,
body,
context: { foo },
externalDep: externalDep()
};
});
// server/middleware/foo-context.ts
export default eventHandler((event) => {
event.context.foo = "bar";
});
// server/utils/external-dep.ts
export function externalDep() {
return "external-value";
} nitro-test-utils created by @johannschopplich is great for performing an // test/routes/api/echo/[value].post.test.ts
import { $fetch } from "nitro-test-utils/e2e";
import { describe, expect, it } from "vitest";
describe("POST /api/echo/{value}", () => {
it("e2e", async () => {
const { data } = await $fetch("/api/echo/data", {
method: "POST",
headers: {
"x-input": "value",
},
body: JSON.stringify({ hello: "world" }),
});
expect(data).toStrictEqual({
method: "POST",
path: "/api/echo/data",
routerParams: {
value: "data",
},
headers: expect.objectContaining({ "x-input": "value" }),
body: { hello: "world" },
context: { foo: "bar" },
externalDep: "external-value",
});
});
}); However, since the nitro server runs in a That being said, I was able to unit test the handler by doing the following:
While this "works", the downsides are:
|
Beta Was this translation helpful? Give feedback.
-
I'm taking a look at https://github.com/unjs/nitro/tree/main/test to see if I can make sense of it and try to come up with my own tests to test my endpoints.
I'm wondering what the best practice method would be to write tests for my Nitro server. I'm thinking of using
vitest
as that's what I see being used in Nitro and H3.Any help would be appreciated.
Beta Was this translation helpful? Give feedback.
All reactions