From 0027a4698c0e2bd90502b8c0a8a40956b97ef211 Mon Sep 17 00:00:00 2001 From: Joaquim Ley Date: Tue, 25 Jun 2024 14:58:22 +0100 Subject: [PATCH 1/2] [TESTING] Add Context section Also adds an example with custom routing. --- docs/guides/testing.md | 56 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/docs/guides/testing.md b/docs/guides/testing.md index 37112247..969dc6f3 100644 --- a/docs/guides/testing.md +++ b/docs/guides/testing.md @@ -128,3 +128,59 @@ test('GET /posts', async () => { const res = await app.request('/posts', {}, MOCK_ENV) }) ``` + +## Context + +To set `c.set()` for testing, you can create a new app and add a custom testing middleware. + +For example, let's add a mock user object to "jwt" to all routes: + +```ts +// Create a mock object +const mockUser = { + id: "550e8400-e29b-41d4-a716-446655440000", + email: "johndoe@example.com", + age: 26 +}; + +let app: Hono; + +beforeEach(() => { + app = new Hono(); + + // Set any test data to your context + app.use("*", async (c, next) => { + c.set("user", mockUser); + await next(); + }); +}); + +``` + +Now during testing your context will have the mock object to which you can make assertions: +```ts +test('GET /friends', async () => { + // When the request is made, the c.get("user") will be the mockUser + const res = await app.request('/friends', MOCK_ENV) + expect(res.status).toBe(200) + expect(respository.getFriends).toHaveBeenCalledWith(mockUser.id); +}) +``` + +You can also test your own routes, make sure to add any middleware before routing: + +```ts +let app: Hono; + +beforeEach(() => { + app = new Hono(); + + app.use("*", async (c, next) => { + // Make changes to your context + await next(); + }); + + // Routing after middleware + app.route("/", myHonoApp); +}); +``` \ No newline at end of file From f352a633e69d464064cdd1cdabedc304c364a3a5 Mon Sep 17 00:00:00 2001 From: Joaquim Ley Date: Sat, 29 Jun 2024 11:09:02 +0100 Subject: [PATCH 2/2] Improve Context example in Guides/Testing --- docs/guides/testing.md | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/docs/guides/testing.md b/docs/guides/testing.md index 969dc6f3..28e6873f 100644 --- a/docs/guides/testing.md +++ b/docs/guides/testing.md @@ -131,24 +131,23 @@ test('GET /posts', async () => { ## Context -To set `c.set()` for testing, you can create a new app and add a custom testing middleware. +To set values into your context `c.set()` for testing, create a custom testing middleware and add it before your test(s) run. For example, let's add a mock user object to "jwt" to all routes: ```ts -// Create a mock object const mockUser = { id: "550e8400-e29b-41d4-a716-446655440000", email: "johndoe@example.com", age: 26 }; +// Create a Hono app instance and set middleware before each test let app: Hono; - beforeEach(() => { app = new Hono(); - // Set any test data to your context + // Create a middleware and set any test data into the context app.use("*", async (c, next) => { c.set("user", mockUser); await next(); @@ -161,17 +160,16 @@ Now during testing your context will have the mock object to which you can make ```ts test('GET /friends', async () => { // When the request is made, the c.get("user") will be the mockUser - const res = await app.request('/friends', MOCK_ENV) + const res = await app.request('/friends', {MOCK_ENV}) expect(res.status).toBe(200) expect(respository.getFriends).toHaveBeenCalledWith(mockUser.id); }) ``` -You can also test your own routes, make sure to add any middleware before routing: +You can also test your route handlers, in this case you must **add the middleware before routing**: ```ts let app: Hono; - beforeEach(() => { app = new Hono(); @@ -180,7 +178,7 @@ beforeEach(() => { await next(); }); - // Routing after middleware + // Routing after any middleware(s) app.route("/", myHonoApp); }); ``` \ No newline at end of file