n8n/packages/cli/test/shared/mocking.ts
कारतोफ्फेलस्क्रिप्ट™ ff77ef4b62
ci: Delete some duplicate code in cli tests (no-changelog) (#9049)
2024-04-05 13:47:49 +02:00

25 lines
797 B
TypeScript

import { Container } from 'typedi';
import { mock } from 'jest-mock-extended';
import type { DeepPartial } from 'ts-essentials';
import { DataSource, EntityManager, type EntityMetadata } from '@n8n/typeorm';
import type { Class } from 'n8n-core';
export const mockInstance = <T>(
serviceClass: Class<T>,
data: DeepPartial<T> | undefined = undefined,
) => {
const instance = mock<T>(data);
Container.set(serviceClass, instance);
return instance;
};
export const mockEntityManager = (entityClass: Class) => {
const entityManager = mockInstance(EntityManager);
const dataSource = mockInstance(DataSource, {
manager: entityManager,
getMetadata: () => mock<EntityMetadata>({ target: entityClass }),
});
Object.assign(entityManager, { connection: dataSource });
return entityManager;
};