Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

refactor: move mailer test (#4328)

Alex Yang74faee887e7b
diff --git a/apps/server/package.json b/apps/server/package.json
index 5257215b1c..42e07b7933 100644
--- a/apps/server/package.json
+++ b/apps/server/package.json
@@ -74,6 +74,7 @@
"yjs": "^13.6.7"
},
"devDependencies": {
+ "@affine-test/kit": "workspace:*",
"@affine/storage": "workspace:*",
"@napi-rs/image": "^1.6.1",
"@nestjs/testing": "^10.2.4",
diff --git a/apps/server/src/tests/mailer.e2e.ts b/apps/server/src/tests/mailer.e2e.ts
index 71d735b39f..fe10cc28f8 100644
--- a/apps/server/src/tests/mailer.e2e.ts
+++ b/apps/server/src/tests/mailer.e2e.ts
@@ -2,6 +2,10 @@
// This test case is for testing the mailer service.
// Please use local SMTP server for testing.
// See: https://github.com/mailhog/MailHog
+import {
+ getCurrentMailMessageCount,
+ getLatestMailMessage,
+} from '@affine-test/kit/utils/cloud';
import { Test, TestingModule } from '@nestjs/testing';
import { PrismaClient } from '@prisma/client';
import test from 'ava';
@@ -13,7 +17,6 @@ import { AuthModule } from '../modules/auth';
import { AuthService } from '../modules/auth/service';
import { PrismaModule } from '../prisma';
import { RateLimiterModule } from '../throttler';
-import { getCurrentMailMessageCount, getLatestMailMessage } from './utils';
let auth: AuthService;
let module: TestingModule;
diff --git a/apps/server/src/tests/utils.ts b/apps/server/src/tests/utils.ts
index 88e988e53d..f7526ba6f9 100644
--- a/apps/server/src/tests/utils.ts
+++ b/apps/server/src/tests/utils.ts
@@ -1,32 +1,16 @@
import { randomUUID } from 'node:crypto';
import type { INestApplication } from '@nestjs/common';
-import { Test } from '@nestjs/testing';
import { hashSync } from '@node-rs/argon2';
import { PrismaClient, User } from '@prisma/client';
-// @ts-expect-error graphql-upload is not typed
-import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
import request from 'supertest';
-import { AppModule } from '../app';
import type { TokenType } from '../modules/auth';
import type { UserType } from '../modules/users';
import type { InvitationType, WorkspaceType } from '../modules/workspaces';
const gql = '/graphql';
-export async function getCurrentMailMessageCount() {
- const response = await fetch('http://localhost:8025/api/v2/messages');
- const data = await response.json();
- return data.total;
-}
-
-export async function getLatestMailMessage() {
- const response = await fetch('http://localhost:8025/api/v2/messages');
- const data = await response.json();
- return data.items[0];
-}
-
async function signUp(
app: INestApplication,
name: string,
@@ -444,7 +428,8 @@ async function flushDB() {
const result: { tablename: string }[] =
await client.$queryRaw`SELECT tablename
FROM pg_catalog.pg_tables
- WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema'`;
+ WHERE schemaname != 'pg_catalog'
+ AND schemaname != 'information_schema'`;
// remove all table data
await client.$executeRawUnsafe(
@@ -457,21 +442,6 @@ async function flushDB() {
await client.$disconnect();
}
-async function createTestApp() {
- const module = await Test.createTestingModule({
- imports: [AppModule],
- }).compile();
- const app = module.createNestApplication();
- app.use(
- graphqlUploadExpress({
- maxFileSize: 10 * 1024 * 1024,
- maxFiles: 5,
- })
- );
- await app.init();
- return app;
-}
-
async function getInviteInfo(
app: INestApplication,
token: string,
@@ -513,6 +483,7 @@ export class FakePrisma {
emailVerified: new Date(),
createdAt: new Date(),
};
+
get user() {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const prisma = this;
@@ -536,7 +507,6 @@ export {
checkBlobSize,
collectAllBlobSizes,
collectBlobSizes,
- createTestApp,
createWorkspace,
currentUser,
flushDB,
diff --git a/apps/server/src/tests/workspace-invite.e2e.ts b/apps/server/src/tests/workspace-invite.e2e.ts
index 6f45dd3742..b38edce434 100644
--- a/apps/server/src/tests/workspace-invite.e2e.ts
+++ b/apps/server/src/tests/workspace-invite.e2e.ts
@@ -1,3 +1,7 @@
+import {
+ getCurrentMailMessageCount,
+ getLatestMailMessage,
+} from '@affine-test/kit/utils/cloud';
import type { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { PrismaClient } from '@prisma/client';
@@ -12,8 +16,6 @@ import {
acceptInvite,
acceptInviteById,
createWorkspace,
- getCurrentMailMessageCount,
- getLatestMailMessage,
getWorkspace,
inviteUser,
leaveWorkspace,
diff --git a/apps/server/tsconfig.node.json b/apps/server/tsconfig.node.json
index e2809e7856..15b75b25d8 100644
--- a/apps/server/tsconfig.node.json
+++ b/apps/server/tsconfig.node.json
@@ -13,6 +13,9 @@
"references": [
{
"path": "../../tests/fixtures"
+ },
+ {
+ "path": "../../tests/kit"
}
],
"include": ["scripts", "package.json"]
diff --git a/tests/kit/tsconfig.json b/tests/kit/tsconfig.json
index d661639f21..cdcdf6248d 100644
--- a/tests/kit/tsconfig.json
+++ b/tests/kit/tsconfig.json
@@ -5,10 +5,5 @@
"noEmit": false,
"outDir": "lib"
},
- "include": ["./*.ts", "utils"],
- "references": [
- {
- "path": "../../apps/server"
- }
- ]
+ "include": ["./*.ts", "utils"]
}
diff --git a/tests/kit/utils/cloud.ts b/tests/kit/utils/cloud.ts
index c9600b116b..0b8a3f498e 100644
--- a/tests/kit/utils/cloud.ts
+++ b/tests/kit/utils/cloud.ts
@@ -12,6 +12,18 @@ import { hash } from '@node-rs/argon2';
import type { BrowserContext, Cookie, Page } from '@playwright/test';
import { z } from 'zod';
+export async function getCurrentMailMessageCount() {
+ const response = await fetch('http://localhost:8025/api/v2/messages');
+ const data = await response.json();
+ return data.total;
+}
+
+export async function getLatestMailMessage() {
+ const response = await fetch('http://localhost:8025/api/v2/messages');
+ const data = await response.json();
+ return data.items[0];
+}
+
export async function getLoginCookie(
context: BrowserContext
): Promise<Cookie | undefined> {
diff --git a/yarn.lock b/yarn.lock
index 114f1de54a..466a490a45 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -653,6 +653,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@affine/server@workspace:apps/server"
dependencies:
+ "@affine-test/kit": "workspace:*"
"@affine/storage": "workspace:*"
"@apollo/server": ^4.9.3
"@auth/prisma-adapter": ^1.0.1