feat: implement e2e test for CompanyResolver (#944)
* feat: wip e2e server test * feat: use github action postgres & use infra for local * feat: company e2e test * feat: add company e2e test for permissions * Simplify server e2e test run * Fix lint --------- Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
41
server/test/utils/check-db.ts
Normal file
41
server/test/utils/check-db.ts
Normal file
@ -0,0 +1,41 @@
|
||||
// check-db.ts
|
||||
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
const schemaDatabaseExists = async (databaseName: string) => {
|
||||
try {
|
||||
const result = await prisma.$queryRawUnsafe<[any]>(
|
||||
`SELECT 1 FROM pg_database WHERE datname = '${databaseName}';`,
|
||||
);
|
||||
|
||||
return result.length > 0;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
async function main() {
|
||||
const databaseName = 'tests';
|
||||
// Check if schema exists
|
||||
const databaseExistsResult = await schemaDatabaseExists(databaseName);
|
||||
|
||||
if (!databaseExistsResult) {
|
||||
throw new Error(`Schema ${databaseName} does not exist`);
|
||||
}
|
||||
|
||||
// Check if database is initialized
|
||||
await prisma.$queryRaw`SELECT 1 FROM pg_tables WHERE tablename='_prisma_migrations';`;
|
||||
}
|
||||
|
||||
main()
|
||||
.then(() => {
|
||||
process.exit(0);
|
||||
})
|
||||
.catch(() => {
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
56
server/test/utils/create-app.ts
Normal file
56
server/test/utils/create-app.ts
Normal file
@ -0,0 +1,56 @@
|
||||
import { NestExpressApplication } from '@nestjs/platform-express';
|
||||
import { Test, TestingModule, TestingModuleBuilder } from '@nestjs/testing';
|
||||
|
||||
import mockUser from 'test/mock-data/user.json';
|
||||
import mockWorkspace from 'test/mock-data/workspace.json';
|
||||
import { RequestHandler } from 'express';
|
||||
|
||||
import { AppModule } from 'src/app.module';
|
||||
|
||||
interface TestingModuleCreatePreHook {
|
||||
(moduleBuilder: TestingModuleBuilder): TestingModuleBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook for adding items to nest application
|
||||
*/
|
||||
export type TestingAppCreatePreHook = (
|
||||
app: NestExpressApplication,
|
||||
) => Promise<void>;
|
||||
|
||||
/**
|
||||
* Sets basic e2e testing module of app
|
||||
*/
|
||||
export async function createApp(
|
||||
config: {
|
||||
moduleBuilderHook?: TestingModuleCreatePreHook;
|
||||
appInitHook?: TestingAppCreatePreHook;
|
||||
} = {},
|
||||
): Promise<[NestExpressApplication, TestingModule]> {
|
||||
let moduleBuilder: TestingModuleBuilder = Test.createTestingModule({
|
||||
imports: [AppModule],
|
||||
});
|
||||
|
||||
if (!!config.moduleBuilderHook) {
|
||||
moduleBuilder = config.moduleBuilderHook(moduleBuilder);
|
||||
}
|
||||
|
||||
const moduleFixture: TestingModule = await moduleBuilder.compile();
|
||||
const app = moduleFixture.createNestApplication<NestExpressApplication>();
|
||||
|
||||
if (config.appInitHook) {
|
||||
await config.appInitHook(app);
|
||||
}
|
||||
|
||||
const mockAuthHandler: RequestHandler = (req, _res, next) => {
|
||||
req.user = {
|
||||
user: mockUser,
|
||||
workspace: mockWorkspace,
|
||||
};
|
||||
next();
|
||||
};
|
||||
|
||||
app.use(mockAuthHandler);
|
||||
|
||||
return [await app.init(), moduleFixture];
|
||||
}
|
||||
18
server/test/utils/reset-db.ts
Normal file
18
server/test/utils/reset-db.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { PrismaClient, Prisma } from '@prisma/client';
|
||||
|
||||
import { camelCase } from 'src/utils/camel-case';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
export default async () => {
|
||||
const models = Prisma.dmmf.datamodel.models;
|
||||
const modelNames = models.map((model) => model.name);
|
||||
const entities = modelNames.map((modelName) => camelCase(modelName));
|
||||
|
||||
await prisma.$transaction(
|
||||
entities.map((entity) => {
|
||||
console.log('entity: ', entity);
|
||||
return prisma[entity].deleteMany();
|
||||
}),
|
||||
);
|
||||
};
|
||||
5
server/test/utils/setup-tests.ts
Normal file
5
server/test/utils/setup-tests.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import resetDb from './reset-db';
|
||||
|
||||
global.beforeEach(() => {
|
||||
// resetDb();
|
||||
});
|
||||
Reference in New Issue
Block a user