-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: templates of different test types implemented * feat: working unit test * fix(ci): getting stuck on e2e tests * fix(test): minor fixes
- Loading branch information
Showing
14 changed files
with
234 additions
and
53 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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,30 +1,29 @@ | ||
import { ContextIdFactory } from "@nestjs/core"; | ||
import { Test, TestingModule } from "@nestjs/testing"; | ||
import { AppModule } from "./app.module"; | ||
import { TestManager } from "../test/TestManager"; | ||
import { AppResolver } from "./app.resolver"; | ||
import { AppService } from "./app.service"; | ||
import { DrizzleModule } from "./drizzle/drizzle.module"; | ||
|
||
describe("AppService", () => { | ||
describe("[GraphQL] [IntegrationTesting] AppResolver", () => { | ||
let testManager = new TestManager(); | ||
let appResolver: AppResolver; | ||
|
||
beforeEach(async () => { | ||
// TODO: create proper test module / class. | ||
const app: TestingModule = await Test.createTestingModule({ | ||
imports: [AppModule], | ||
}).compile(); | ||
beforeAll(async () => { | ||
await testManager.beforeAll(); | ||
|
||
// TODO: are there a better handling for this? @xUser5000 | ||
const contextId = ContextIdFactory.create(); | ||
jest.spyOn(ContextIdFactory, "getByRequest").mockImplementation( | ||
() => contextId, | ||
); | ||
|
||
appResolver = await app.resolve(AppResolver, contextId); | ||
appResolver = await testManager.app.resolve(AppResolver, contextId); | ||
}); | ||
|
||
describe("root", () => { | ||
it('should return "Hello World!"', () => { | ||
expect(appResolver.hello()).toBe("Hello World!"); | ||
}); | ||
afterAll(async () => { | ||
await testManager.afterAll(); | ||
}); | ||
|
||
it('should return "Hello World!"', async () => { | ||
const result = appResolver.hello(); | ||
expect(result).toBe("Hello World!"); | ||
}); | ||
}); |
This file contains 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 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,44 @@ | ||
import { Test, TestingModule } from "@nestjs/testing"; | ||
import { AppService } from "./app.service"; | ||
import { DrizzleService } from "./drizzle/drizzle.service"; | ||
|
||
describe("[GraphQL] [UnitTesting] AppService", () => { | ||
let appService: AppService; | ||
let drizzleService: DrizzleService; | ||
|
||
// Note: we *shouldn't* (?) need TestManager for unit tests. | ||
beforeAll(async () => { | ||
const app: TestingModule = await Test.createTestingModule({ | ||
// Mocking can happen here (if appService has dependencies), | ||
// or add specific mocks to each test case. | ||
providers: [ | ||
AppService, | ||
{ | ||
provide: DrizzleService, | ||
useValue: { | ||
db: { | ||
query: { | ||
users: { | ||
findMany: jest.fn().mockReturnValue([]), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
drizzleService = app.get<DrizzleService>(DrizzleService); | ||
appService = app.get<AppService>(AppService); | ||
}); | ||
|
||
afterAll(async () => {}); | ||
|
||
it('should return "Hello World!"', async () => { | ||
const spy = jest.spyOn(drizzleService.db.query.users, "findMany"); | ||
|
||
const result = await appService.testDb(); | ||
expect(spy).toHaveBeenCalled(); | ||
expect(result).toStrictEqual([]); | ||
}); | ||
}); |
This file contains 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 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 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,64 @@ | ||
import { INestApplication, ValidationPipe } from "@nestjs/common"; | ||
import { ConfigService } from "@nestjs/config"; | ||
import { NestExpressApplication } from "@nestjs/platform-express"; | ||
import { Test } from "@nestjs/testing"; | ||
import RedisStore from "connect-redis"; | ||
import * as session from "express-session"; | ||
import * as passport from "passport"; | ||
import { createClient } from "redis"; | ||
import { AppModule } from "../src/app.module"; | ||
|
||
export class TestManager { | ||
// biome-ignore lint/suspicious/noExplicitAny: it is any. | ||
public httpServer: any; | ||
public app: INestApplication<NestExpressApplication>; | ||
|
||
// TODO: Find a way to abstract this logic, found in main.ts too. | ||
async beforeAll(): Promise<void> { | ||
const moduleRef = await Test.createTestingModule({ | ||
imports: [AppModule], | ||
}).compile(); | ||
this.app = moduleRef.createNestApplication(); | ||
|
||
const configService = this.app.get(ConfigService); | ||
|
||
this.app.useGlobalPipes( | ||
new ValidationPipe({ | ||
transform: true, | ||
whitelist: true, | ||
}), | ||
); | ||
|
||
const redisClient = await createClient({ | ||
url: String(configService.getOrThrow("REDIS_URL")), | ||
}).connect(); | ||
|
||
this.app.use( | ||
session({ | ||
secret: configService.getOrThrow<string>("SESSION_SECRET"), | ||
resave: false, | ||
saveUninitialized: false, | ||
cookie: { | ||
maxAge: configService.getOrThrow<number>("COOKIE_MAX_AGE"), | ||
httpOnly: true, | ||
}, | ||
store: new RedisStore({ | ||
client: redisClient, | ||
}), | ||
}), | ||
); | ||
|
||
this.app.use(passport.initialize()); | ||
this.app.use(passport.session()); | ||
this.app.enableCors(); | ||
|
||
this.httpServer = this.app.getHttpServer(); | ||
await this.app.init(); | ||
} | ||
|
||
async afterAll() { | ||
await this.app.close(); | ||
} | ||
|
||
// Helper functions can be added here if needed e.g. generateUser(). | ||
} |
This file contains 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 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 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,23 @@ | ||
import "tsconfig-paths/register"; | ||
|
||
import { createClient } from "redis"; | ||
import { TestManager } from "./TestManager"; | ||
|
||
export default async (): Promise<void> => { | ||
console.log("# Started Jest globalSetup."); | ||
const testManager = new TestManager(); | ||
|
||
await testManager.beforeAll(); | ||
|
||
await testManager.app.init(); | ||
|
||
// TODO: Apply Database migrations/seeders. | ||
|
||
await testManager.app.close(); | ||
|
||
// Delete records in redis. | ||
const client = createClient(); | ||
await client.connect(); | ||
await client.flushAll(); | ||
console.log("# Finished Jest globalSetup."); | ||
}; |
Oops, something went wrong.