2024-04-05 04:47:49 -07:00
|
|
|
import { DataSource, EntityManager, type EntityMetadata } from '@n8n/typeorm';
|
2024-09-12 09:07:18 -07:00
|
|
|
import { mock } from 'jest-mock-extended';
|
2023-12-27 02:50:43 -08:00
|
|
|
import type { Class } from 'n8n-core';
|
2024-09-12 09:07:18 -07:00
|
|
|
import type { DeepPartial } from 'ts-essentials';
|
|
|
|
import { Container } from 'typedi';
|
2023-07-13 01:14:48 -07:00
|
|
|
|
2024-10-09 03:56:31 -07:00
|
|
|
import type { Logger } from '@/logging/logger.service';
|
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
export const mockInstance = <T>(
|
2023-12-27 02:50:43 -08:00
|
|
|
serviceClass: Class<T>,
|
2023-07-13 01:14:48 -07:00
|
|
|
data: DeepPartial<T> | undefined = undefined,
|
|
|
|
) => {
|
|
|
|
const instance = mock<T>(data);
|
2023-12-27 02:50:43 -08:00
|
|
|
Container.set(serviceClass, instance);
|
2023-07-13 01:14:48 -07:00
|
|
|
return instance;
|
|
|
|
};
|
2024-04-05 04:47:49 -07:00
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
2024-10-09 03:56:31 -07:00
|
|
|
|
|
|
|
export const mockLogger = () =>
|
|
|
|
mock<Logger>({ withScope: jest.fn().mockReturnValue(mock<Logger>()) });
|