mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-17 10:06:17 +08:00
test(server): make testing more isolated (#4290)
This commit is contained in:
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user