I am trying to mock my controller using nestjs createTestingModule and I have added overrider provider to mock the service but it still giving me this error(Screenshot attached screenshot). Kindly help.
I also tried added the other dependencies too but the error is same
Here is my controller.ts file
import {Body, NotFoundException, OnModuleInit, Param, Patch} from "@nestjs/common";
import {BitvavoService} from "./bitvavo.service";
import {DeleteMethod, GetMethod, PostMethod, PatchMethod} from "../../../../global/operations";
import {ApiController} from "../../../../utils/endpoints/api-controller.decorator";
import bitvavo, {BitvavoOrderResponse} from "bitvavo";
import {Repository} from "../../../repository/repository";
import {BitvavoTarget} from "./bitvavo-target";
import {RepositoryFactory} from "../../../repository/repository-factory.service";
import {CollectionNames} from "../../../repository/collection-names";
import {ProducerTargetBindingService} from "../../producer-target-binding/producer-target-binding.service";
import {User, UserClass} from "../../../../utils/endpoints/user.decorator";
import {Permissions} from "../../../../utils/endpoints/permissions.decorator";
import {Permission} from "../../../../utils/permission.enum";
import {EncryptionService} from "../../../encrypt/encrypt.service";
import {
BitvavoBalanceDto,
BitvavoPlaceOrderDto,
BitvavoSingleTargetResponse,
CreateBitvavoTargetDto,
UpdateBitvavoTargetDto,
ChangeBitvavoTargetMode,
SellSingleBot
} from "./bitvavo-dtos";
import {toTargetDto} from "../target-dto";
import {ApiBotService} from "../../signal-producers/implementers/api-bot/api-bot.service";
const getBalances = async function (target: BitvavoTarget, decryptedKey: string, decryptedSecret: string) {
try {
const client = createBitvavoClient(decryptedKey, decryptedSecret);
return (await client.balance({})).map(balanceToDto);
} catch (e) {
return null;
}
};
@ApiController("signal-targets/bitvavo")
export class BitvavoController implements OnModuleInit {
private repository: Repository<BitvavoTarget>;
async onModuleInit() {
this.repository = await this.repositoryFactory.create(BitvavoTarget);
}
constructor(
private bitvavoService: BitvavoService,
private repositoryFactory: RepositoryFactory,
private producerTargetBindingService: ProducerTargetBindingService,
private encryptionService: EncryptionService,
private apiBotService: ApiBotService
) {}
@PostMethod()
@Permissions(Permission.CreateTargets)
async create(@Body() targetDto: CreateBitvavoTargetDto, @User() user: UserClass) {
const userTargets = await this.repository.getAll(user.sub);
const safeModel = {
...targetDto,
name: targetDto.name ? targetDto.name : `Bitvavo Wallet ${userTargets.length + 1}`,
key: this.encryptionService.encrypt(targetDto.key),
secret: this.encryptionService.encrypt(targetDto.secret)
};
await this.repository.create({...safeModel, userId: user.sub, positions: []}); // TODO some unique checking stuff
}
@GetMethod()
@Permissions(Permission.ReadTargets)
async get(@User() user: UserClass) {
if (user.permissions.includes(Permission.AdminTargets)) {
return await this.repository.getAllInternal();
}
return await this.repository.getAll(user.sub);
}
@GetMethod("/:id")
@Permissions(Permission.ReadTargets)
async getOne(@Param("id") targetId: string, @User() user: UserClass): Promise<BitvavoSingleTargetResponse> {
let targetOpt;
if (user.permissions.includes(Permission.AdminTargets)) {
targetOpt = await this.bitvavoService.getOneInternal(targetId);
} else {
targetOpt = await this.bitvavoService.getOne(targetId, user.sub);
}
if (!targetOpt) {
throw new NotFoundException();
}
const {bindings, target} = targetOpt;
const decryptedKey = this.encryptionService.decrypt(target.key);
const decryptedSecret = this.encryptionService.decrypt(target.secret);
const balances = await getBalances(target, decryptedKey, decryptedSecret);
return {
target: toTargetDto({type: CollectionNames.Bitvavo, name: target.name, _id: target._id, targetMode: target.targetMode}),
// producer: mapProducerToDto(producer),
balances: balances,
bindings: bindings
};
}
@PostMethod("/:id/placeOrder")
async placeOrder(@Param("id") id: string, @Body() body: BitvavoPlaceOrderDto, @User() user: UserClass): Promise<BitvavoOrderResponse> {
let target;
if (user.permissions.includes(Permission.AdminTargets)) {
target = await this.repository.getOneByIdInternal(id);
} else {
target = await this.repository.getOneById(id, user.sub);
}
if (!target) {
throw new NotFoundException();
}
return await this.bitvavoService.handleSignal(target, body);
}
@Patch("/:id")
@Permissions(Permission.EditTargets)
async update(@Param("id") id: string, @User() user: UserClass, @Body() targetDto: UpdateBitvavoTargetDto) {
let target;
let key;
let secret;
if (user.permissions.includes(Permission.AdminTargets)) {
target = await this.repository.getOneModelByIdInternal(id);
} else {
target = await this.repository.getOneModelById(id, user.sub);
}
if (!target) {
throw new NotFoundException();
}
if (targetDto.key) {
key = this.encryptionService.encrypt(targetDto.key);
} else {
key = target.key;
}
if (targetDto.secret) {
secret = this.encryptionService.encrypt(targetDto.secret);
} else {
secret = target.secret;
}
Object.assign(target, {...targetDto, key, secret});
await target.save();
}
@DeleteMethod("/:id")
@Permissions(Permission.DeleteTargets)
async deleteTarget(@Param("id") id: string, @User() user: UserClass) {
let target;
if (user.permissions.includes(Permission.AdminTargets)) {
target = await this.repository.getOneModelByIdInternal(id);
} else {
target = await this.repository.getOneModelById(id, user.sub);
}
if (!target) {
throw new NotFoundException();
}
await this.repository.deleteById(id);
await this.producerTargetBindingService.deleteAllForTarget({id: target.id, type: CollectionNames.Bitvavo});
}
@PostMethod("/:id/sellto/:base")
@Permissions(Permission.EditTargets)
async sellAllForTarget(@Param("id") id: string, @Param("base") base: string, @User() user: UserClass) {
let target;
if (user.permissions.includes(Permission.AdminTargets)) {
target = await this.repository.getOneModelByIdInternal(id);
} else {
target = await this.repository.getOneModelById(id, user.sub);
}
if (!target) {
throw new NotFoundException();
}
await this.producerTargetBindingService.deleteAllForTarget({id: target.id, type: CollectionNames.Bitvavo});
await this.bitvavoService.sellAllToAsset(target, base);
}
@PatchMethod("/:id/changeTargetMode")
@Permissions(Permission.EditTargets)
async changeTargetMode(@Param("id") id: string, @Body() targetDto: ChangeBitvavoTargetMode, @User() user: UserClass) {
let target;
if (user.permissions.includes(Permission.AdminTargets)) {
target = await this.repository.getOneModelByIdInternal(id);
} else {
target = await this.repository.getOneModelById(id, user.sub);
}
if (!target) {
throw new NotFoundException();
}
Object.assign(target, {...target, targetMode: targetDto.targetMode});
await target.save();
}
@PostMethod("/:id/stopBot/:base")
@Permissions(Permission.EditTargets)
async sellSingleTarget(@Param("id") id: string, @Param("base") base: string, @Body() body: SellSingleBot, @User() user: UserClass) {
let target;
let producer;
if (user.permissions.includes(Permission.AdminTargets)) {
target = await this.repository.getOneModelByIdInternal(id);
} else {
target = await this.repository.getOneModelById(id, user.sub);
}
producer = await this.apiBotService.getOneModelInternal(body.botName);
if (!target || !producer) {
throw new NotFoundException();
}
await this.producerTargetBindingService.deleteSingleForTarget(
{id: target.id, type: CollectionNames.Bitvavo},
{id: producer._id, type: CollectionNames.ApiBot}
);
await this.bitvavoService.sellAllToAsset(target, base);
}
}
export const createBitvavoClient = (key: string, secret: string) => {
return bitvavo().options({
APIKEY: key,
APISECRET: secret,
ACCESSWINDOW: 10000,
RESTURL: "https://api.bitvavo.com/v2",
WSURL: "wss://ws.bitvavo.com/v2/",
DEBUGGING: false
});
};
const balanceToDto = (balance: {symbol: string; available: string; inOrder: string}): BitvavoBalanceDto => {
return balance as BitvavoBalanceDto;
};
Here is my controller.spec.ts
import {BitvavoService} from "./bitvavo.service";
import {Test} from "@nestjs/testing";
import {BitvavoController} from "./bitvavo.controller";
describe("Bitvavo Controller", () => {
let controller: BitvavoController;
const mockValue = {};
beforeEach(async () => {
const moduleRef = await Test.createTestingModule({
controllers: [BitvavoController],
providers: [BitvavoService]
})
.overrideProvider(BitvavoService)
.useValue({onModuleInit: jest.fn(), handleSignal: jest.fn(), sellAllToAsset: jest.fn(), getOne: jest.fn(), getOneInternal: jest.fn()})
.compile();
});
it("test", () => {});
});