-
Notifications
You must be signed in to change notification settings - Fork 1
health module and vite 8 migration #151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { beforeEach, describe, expect, it, Mock, vi } from "vitest"; | ||
|
|
||
| import { Test } from "@nestjs/testing"; | ||
| import { Procedure } from "@vitest/spy"; | ||
|
|
||
| import { HealthController } from "./health.controller"; | ||
| import { HealthService } from "./health.service"; | ||
|
|
||
| describe("HealthController", () => { | ||
| let healthController: HealthController; | ||
|
|
||
| let healthService: { | ||
| ping: Mock<Procedure>; | ||
| }; | ||
|
|
||
| beforeEach(async () => { | ||
| vi.clearAllMocks(); | ||
|
|
||
| healthService = { ping: vi.fn() }; | ||
|
|
||
| const moduleRef = await Test.createTestingModule({ | ||
| providers: [ | ||
| HealthController, | ||
| { | ||
| provide: HealthService, | ||
| useValue: healthService, | ||
| }, | ||
| ], | ||
| }).compile(); | ||
|
|
||
| healthController = moduleRef.get(HealthController); | ||
| }); | ||
|
|
||
| describe("ping", () => { | ||
| it("should call the ping method of the healthService", () => { | ||
| healthController.ping(); | ||
|
|
||
| expect(healthService.ping).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { Controller, Get } from "@nestjs/common"; | ||
|
|
||
| import { HealthService } from "./health.service"; | ||
|
|
||
| @Controller("health") | ||
| export class HealthController { | ||
| constructor(private readonly healthService: HealthService) {} | ||
|
|
||
| @Get("ping") | ||
| ping() { | ||
| return this.healthService.ping(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { Module } from "@nestjs/common"; | ||
|
|
||
| import { HealthController } from "./health.controller"; | ||
| import { HealthRouter } from "./health.router"; | ||
| import { HealthService } from "./health.service"; | ||
|
|
||
| @Module({ | ||
| controllers: [HealthController], | ||
| providers: [HealthService, HealthRouter], | ||
| exports: [HealthRouter], | ||
| }) | ||
| export class HealthModule {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import { Request, Response } from "express"; | ||
| import { beforeEach, describe, expect, it, Mock, vi } from "vitest"; | ||
|
|
||
| import { TrpcModule } from "@/trpc/trpc.module"; | ||
| import { TrpcService } from "@/trpc/trpc.service"; | ||
| import { Test } from "@nestjs/testing"; | ||
| import { Procedure } from "@vitest/spy"; | ||
|
|
||
| import { HealthRouter } from "./health.router"; | ||
| import { HealthService } from "./health.service"; | ||
|
|
||
| describe("HealthRouter", () => { | ||
| let healthRouter: HealthRouter; | ||
| let trpcService: TrpcService; | ||
|
|
||
| let healthService: { | ||
| ping: Mock<Procedure>; | ||
| }; | ||
|
|
||
| const mockedCtx = { | ||
| req: { | ||
| headers: { | ||
| "x-forwarded-for": "", | ||
| } as Record<string, string>, | ||
| } as Request, | ||
|
|
||
| res: { | ||
| cookie: vi.fn() as Function, | ||
| clearCookie: vi.fn() as Function, | ||
| } as Response, | ||
| }; | ||
|
|
||
| const mockedPayload = { | ||
| sub: "01kv1aqeffy49vc8bzq19nwvhh", | ||
| iat: 1780458967, | ||
| exp: 1782878167, | ||
| }; | ||
|
|
||
| let caller: ReturnType< | ||
| ReturnType<HealthRouter["procedures"]>["health"]["createCaller"] | ||
| >; | ||
|
|
||
| beforeEach(async () => { | ||
| vi.clearAllMocks(); | ||
|
|
||
| healthService = { | ||
| ping: vi.fn(), | ||
| }; | ||
|
|
||
| const moduleRef = await Test.createTestingModule({ | ||
| imports: [TrpcModule], | ||
| providers: [ | ||
| HealthRouter, | ||
| { | ||
| provide: HealthService, | ||
| useValue: healthService, | ||
| }, | ||
| ], | ||
| }).compile(); | ||
|
|
||
| healthRouter = moduleRef.get(HealthRouter); | ||
| trpcService = moduleRef.get(TrpcService); | ||
|
|
||
| caller = trpcService.trpc.createCallerFactory( | ||
| healthRouter.procedures().health, | ||
| )(mockedCtx); | ||
| vi.spyOn(trpcService, "getPayload").mockResolvedValue(mockedPayload); | ||
| }); | ||
|
|
||
| describe("ping", () => { | ||
| const mockedOutput = { | ||
| status: "OK", | ||
| }; | ||
|
|
||
| it("should call the ping method of the healthService", async () => { | ||
| healthService.ping.mockReturnValue(mockedOutput); | ||
|
|
||
| await caller.ping(); | ||
|
|
||
| expect(healthService.ping).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("should return a status object", async () => { | ||
| healthService.ping.mockReturnValue(mockedOutput); | ||
|
|
||
| const statusObj = await caller.ping(); | ||
|
|
||
| expect(statusObj).toBeDefined(); | ||
| expect(statusObj).toEqual(mockedOutput); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { TrpcService } from "@/trpc/trpc.service"; | ||
| import { Injectable } from "@nestjs/common"; | ||
|
|
||
| import { HealthService } from "./health.service"; | ||
|
|
||
| @Injectable() | ||
| export class HealthRouter { | ||
| constructor( | ||
| private readonly healthService: HealthService, | ||
| private readonly trpcService: TrpcService, | ||
| ) {} | ||
|
|
||
| procedures() { | ||
| return { | ||
| health: this.trpcService.trpc.router({ | ||
| ping: this.trpcService | ||
| .publicProcedure() | ||
| .query(() => this.healthService.ping()), | ||
| }), | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import { Test } from "@nestjs/testing"; | ||
|
|
||
| import { HealthService } from "./health.service"; | ||
|
|
||
| describe("HealthService", () => { | ||
| let healthService: HealthService; | ||
|
|
||
| beforeEach(async () => { | ||
| vi.clearAllMocks(); | ||
|
|
||
| const moduleRef = await Test.createTestingModule({ | ||
| providers: [HealthService], | ||
| }).compile(); | ||
|
|
||
| healthService = moduleRef.get(HealthService); | ||
| }); | ||
|
|
||
| describe("ping", () => { | ||
| it("should return an OK status", () => { | ||
| const { status } = healthService.ping(); | ||
| expect(status).toBeDefined(); | ||
| expect(status).toEqual("OK"); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { Injectable } from "@nestjs/common"; | ||
|
|
||
| @Injectable() | ||
| export class HealthService { | ||
| ping() { | ||
| return { status: "OK" }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,13 @@ | ||
| import { z } from "zod"; | ||
|
|
||
| import { IsoDateStringSchema } from "@repo/common/types-schemas"; | ||
|
|
||
| import { PERIODS } from "./constants"; | ||
|
|
||
| export const PeriodSchema = z.enum([...PERIODS]); | ||
|
|
||
| export const IsoDateSchema = IsoDateStringSchema.transform( | ||
| (dateStr) => new Date(dateStr), | ||
| ); | ||
|
|
||
| export type Period = z.infer<typeof PeriodSchema>; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.