test(server): make testing more isolated (#4290)

This commit is contained in:
LongYinan
2023-09-08 13:02:27 -07:00
committed by GitHub
parent a97fd486c3
commit 58a935b31d
4 changed files with 49 additions and 32 deletions
+22 -18
View File
@@ -1,40 +1,44 @@
/// <reference types="../global.d.ts" />
import { Test, TestingModule } from '@nestjs/testing';
import { PrismaClient } from '@prisma/client';
import test from 'ava';
import ava, { TestFn } from 'ava';
import { ConfigModule } from '../config';
import { SessionModule, SessionService } from '../session';
let session: SessionService;
let module: TestingModule;
const test = ava as TestFn<{
session: SessionService;
app: TestingModule;
}>;
// cleanup database before each test
test.beforeEach(async () => {
const client = new PrismaClient();
await client.$connect();
await client.user.deleteMany({});
await client.$disconnect();
});
test.beforeEach(async () => {
module = await Test.createTestingModule({
imports: [ConfigModule.forRoot(), SessionModule],
test.beforeEach(async t => {
const module = await Test.createTestingModule({
imports: [
ConfigModule.forRoot({
redis: {
enabled: false,
},
}),
SessionModule,
],
}).compile();
session = module.get(SessionService);
const session = module.get(SessionService);
t.context.app = module;
t.context.session = session;
});
test.afterEach(async () => {
await module.close();
test.afterEach(async t => {
await t.context.app.close();
});
test('should be able to set session', async t => {
const { session } = t.context;
await session.set('test', 'value');
t.is(await session.get('test'), 'value');
});
test('should be expired by ttl', async t => {
const { session } = t.context;
await session.set('test', 'value', 100);
t.is(await session.get('test'), 'value');
await new Promise(resolve => setTimeout(resolve, 500));