mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
feat: add unit tests global provider config
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024 Hexastack. All rights reserved.
|
||||
* Copyright © 2025 Hexastack. All rights reserved.
|
||||
*
|
||||
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
|
||||
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
|
||||
@@ -7,7 +7,6 @@
|
||||
*/
|
||||
|
||||
import { getModelToken } from '@nestjs/mongoose';
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { Model, Types } from 'mongoose';
|
||||
|
||||
import { DummyRepository } from '@/utils/test/dummy/repositories/dummy.repository';
|
||||
@@ -15,6 +14,7 @@ import { closeInMongodConnection } from '@/utils/test/test';
|
||||
|
||||
import { DummyModule } from '../test/dummy/dummy.module';
|
||||
import { Dummy } from '../test/dummy/schemas/dummy.schema';
|
||||
import { buildTestingMocks } from '../test/utils';
|
||||
|
||||
describe('BaseRepository', () => {
|
||||
let dummyModel: Model<Dummy>;
|
||||
@@ -22,11 +22,13 @@ describe('BaseRepository', () => {
|
||||
let createdId: string;
|
||||
|
||||
beforeAll(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
const { getMocks } = await buildTestingMocks({
|
||||
imports: [DummyModule],
|
||||
}).compile();
|
||||
dummyModel = module.get<Model<Dummy>>(getModelToken(Dummy.name));
|
||||
dummyRepository = module.get<DummyRepository>(DummyRepository);
|
||||
});
|
||||
[dummyRepository, dummyModel] = await getMocks([
|
||||
DummyRepository,
|
||||
getModelToken(Dummy.name),
|
||||
]);
|
||||
});
|
||||
afterEach(jest.clearAllMocks);
|
||||
afterAll(closeInMongodConnection);
|
||||
|
||||
@@ -6,13 +6,12 @@
|
||||
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
|
||||
*/
|
||||
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
|
||||
import { DummyService } from '@/utils/test/dummy/services/dummy.service';
|
||||
import { closeInMongodConnection } from '@/utils/test/test';
|
||||
|
||||
import { DummyModule } from '../test/dummy/dummy.module';
|
||||
import { DummyRepository } from '../test/dummy/repositories/dummy.repository';
|
||||
import { buildTestingMocks } from '../test/utils';
|
||||
|
||||
describe('BaseService', () => {
|
||||
let dummyRepository: DummyRepository;
|
||||
@@ -30,11 +29,13 @@ describe('BaseService', () => {
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
const { getMocks } = await buildTestingMocks({
|
||||
imports: [DummyModule],
|
||||
}).compile();
|
||||
dummyRepository = module.get<DummyRepository>(DummyRepository);
|
||||
dummyService = module.get<DummyService>(DummyService);
|
||||
});
|
||||
[dummyRepository, dummyService] = await getMocks([
|
||||
DummyRepository,
|
||||
DummyService,
|
||||
]);
|
||||
});
|
||||
afterEach(jest.clearAllMocks);
|
||||
afterAll(closeInMongodConnection);
|
||||
|
||||
53
api/src/utils/test/utils.ts
Normal file
53
api/src/utils/test/utils.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright © 2025 Hexastack. All rights reserved.
|
||||
*
|
||||
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
|
||||
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
|
||||
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
|
||||
*/
|
||||
|
||||
import { ModuleMetadata } from '@nestjs/common';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
|
||||
import { LoggerService } from '@/logger/logger.service';
|
||||
|
||||
type TTypeOrToken = [
|
||||
new (...args: any[]) => any,
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-expect-error
|
||||
...(new (...args: any[]) => any[]),
|
||||
];
|
||||
const findInstances = async <T extends TTypeOrToken>(
|
||||
type: keyof TestingModule,
|
||||
module: TestingModule,
|
||||
typesOrTokens: T,
|
||||
): Promise<{ [K in keyof T]: InstanceType<T[K]> }> =>
|
||||
Promise.all(
|
||||
typesOrTokens.map((typeOrToken) =>
|
||||
module[type.toString()]<InstanceType<typeof typeOrToken>>(typeOrToken),
|
||||
),
|
||||
);
|
||||
|
||||
const extractInstances =
|
||||
(type: keyof TestingModule, module: TestingModule) =>
|
||||
async <T extends TTypeOrToken>(types: T) =>
|
||||
await findInstances(type, module, types);
|
||||
|
||||
export const buildTestingMocks = async ({
|
||||
providers,
|
||||
...rest
|
||||
}: ModuleMetadata) => {
|
||||
const module = await Test.createTestingModule({
|
||||
...rest,
|
||||
...(providers && {
|
||||
providers: [LoggerService, EventEmitter2, ...providers],
|
||||
}),
|
||||
}).compile();
|
||||
|
||||
return {
|
||||
module,
|
||||
getMocks: extractInstances('get', module),
|
||||
resolveMocks: extractInstances('resolve', module),
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user