test(server): make server testing utils (#5544)

This commit is contained in:
liuyi
2024-01-11 06:40:55 +00:00
parent 9253e522aa
commit 12fdb18a80
20 changed files with 238 additions and 451 deletions

View File

@@ -3,20 +3,18 @@ import {
getLatestMailMessage,
} from '@affine-test/kit/utils/cloud';
import type { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { PrismaClient } from '@prisma/client';
import ava, { type TestFn } from 'ava';
import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
import { AppModule } from '../src/app';
import { RevertCommand, RunCommand } from '../src/data/commands/run';
import { MailService } from '../src/modules/auth/mailer';
import { AuthService } from '../src/modules/auth/service';
import { PrismaService } from '../src/prisma';
import {
acceptInviteById,
createTestingApp,
createWorkspace,
getWorkspace,
initFeatureConfigs,
inviteUser,
leaveWorkspace,
revokeUser,
@@ -31,36 +29,13 @@ const test = ava as TestFn<{
}>;
test.beforeEach(async t => {
const client = new PrismaClient();
t.context.client = client;
await client.$connect();
await client.user.deleteMany({});
await client.snapshot.deleteMany({});
await client.update.deleteMany({});
await client.workspace.deleteMany({});
await client.$disconnect();
const module = await Test.createTestingModule({
const { app } = await createTestingApp({
imports: [AppModule],
providers: [RevertCommand, RunCommand],
}).compile();
const app = module.createNestApplication();
app.use(
graphqlUploadExpress({
maxFileSize: 10 * 1024 * 1024,
maxFiles: 5,
})
);
await app.init();
const auth = module.get(AuthService);
const mail = module.get(MailService);
});
t.context.app = app;
t.context.auth = auth;
t.context.mail = mail;
// init features
await initFeatureConfigs(module);
t.context.client = app.get(PrismaService);
t.context.auth = app.get(AuthService);
t.context.mail = app.get(MailService);
});
test.afterEach.always(async t => {