mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-21 12:06:35 +08:00
test(server): make server testing utils (#5544)
This commit is contained in:
@@ -1,14 +1,9 @@
|
||||
/// <reference types="../src/global.d.ts" />
|
||||
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { INestApplication, Injectable } from '@nestjs/common';
|
||||
import ava, { type TestFn } from 'ava';
|
||||
|
||||
import { CacheModule } from '../src/cache';
|
||||
import { ConfigModule } from '../src/config';
|
||||
import { RevertCommand, RunCommand } from '../src/data/commands/run';
|
||||
import { AuthModule } from '../src/modules/auth';
|
||||
import { AuthService } from '../src/modules/auth/service';
|
||||
import {
|
||||
FeatureManagementService,
|
||||
@@ -19,9 +14,8 @@ import {
|
||||
import { UserType } from '../src/modules/users/types';
|
||||
import { WorkspaceResolver } from '../src/modules/workspaces/resolvers';
|
||||
import { Permission } from '../src/modules/workspaces/types';
|
||||
import { PrismaModule, PrismaService } from '../src/prisma';
|
||||
import { RateLimiterModule } from '../src/throttler';
|
||||
import { initFeatureConfigs } from './utils';
|
||||
import { PrismaService } from '../src/prisma';
|
||||
import { createTestingApp } from './utils';
|
||||
|
||||
@Injectable()
|
||||
class WorkspaceResolverMock {
|
||||
@@ -53,20 +47,11 @@ const test = ava as TestFn<{
|
||||
feature: FeatureService;
|
||||
workspace: WorkspaceResolver;
|
||||
management: FeatureManagementService;
|
||||
app: TestingModule;
|
||||
app: INestApplication;
|
||||
}>;
|
||||
|
||||
// cleanup database before each test
|
||||
test.beforeEach(async () => {
|
||||
const client = new PrismaClient();
|
||||
await client.$connect();
|
||||
await client.user.deleteMany({});
|
||||
await client.workspace.deleteMany({});
|
||||
await client.$disconnect();
|
||||
});
|
||||
|
||||
test.beforeEach(async t => {
|
||||
const module = await Test.createTestingModule({
|
||||
const { app } = await createTestingApp({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
auth: {
|
||||
@@ -80,28 +65,21 @@ test.beforeEach(async t => {
|
||||
earlyAccessPreview: true,
|
||||
},
|
||||
}),
|
||||
PrismaModule,
|
||||
CacheModule,
|
||||
AuthModule,
|
||||
FeatureModule,
|
||||
RateLimiterModule,
|
||||
RevertCommand,
|
||||
RunCommand,
|
||||
],
|
||||
providers: [WorkspaceResolver],
|
||||
})
|
||||
.overrideProvider(WorkspaceResolver)
|
||||
.useClass(WorkspaceResolverMock)
|
||||
.compile();
|
||||
tapModule: module => {
|
||||
module
|
||||
.overrideProvider(WorkspaceResolver)
|
||||
.useClass(WorkspaceResolverMock);
|
||||
},
|
||||
});
|
||||
|
||||
t.context.app = module;
|
||||
t.context.auth = module.get(AuthService);
|
||||
t.context.feature = module.get(FeatureService);
|
||||
t.context.workspace = module.get(WorkspaceResolver);
|
||||
t.context.management = module.get(FeatureManagementService);
|
||||
|
||||
// init features
|
||||
await initFeatureConfigs(module);
|
||||
t.context.app = app;
|
||||
t.context.auth = app.get(AuthService);
|
||||
t.context.feature = app.get(FeatureService);
|
||||
t.context.workspace = app.get(WorkspaceResolver);
|
||||
t.context.management = app.get(FeatureManagementService);
|
||||
});
|
||||
|
||||
test.afterEach.always(async t => {
|
||||
|
||||
Reference in New Issue
Block a user