1

Not able to compile unit test setup

Basically I have following setup for test:

import { Test, TestingModule } from '@nestjs/testing';
import { BlockchainModule } from '../blockchain/blockchain.module';

import { BlockchainService } from '../blockchain/blockchain.service';
import { FaucetModule } from '../faucet/faucet.module';
import { FaucetService } from '../faucet/faucet.service';
import { UserController } from './user.controller';

import { UserService } from './user.service';

describe('UserController', () => {
    let controller: UserController;
    let userService: UserService;
    let faucetService: FaucetService;
    let blockchainService: BlockchainService;

    beforeEach(async () => {
        const module: TestingModule = await Test.createTestingModule({
            imports: [BlockchainModule, FaucetModule],
            controllers: [UserController],
            providers: [UserService, FaucetService, BlockchainService],
        }).compile();

        controller = module.get<UserController>(UserController);
        userService = module.get<UserService>(UserService);
        faucetService = module.get<FaucetService>(FaucetService);
        blockchainService = module.get<BlockchainService>(BlockchainService);
    });

    // it('should be defined', () => {
    //     expect(controller).toBeDefined();
    // });
});

And throws following error: Cannot find module 'src/config/app.config' from 'modules/blockchain/blockchain.service.ts' This is how AppConfig is imported in blockchain service:

blockchain.service.ts:

import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { Wallet, providers, utils, Overrides } from 'ethers';
import { AppConfig } from 'src/config/app.config';
import { OpnCash } from './contract/OpnCash';
import { OpnCashFactory } from './contract/OpnCashFactory';

import { Erc20Factory } from './contract/Erc20Factory';

@Injectable()
export class BlockchainService {
    private contract: OpnCash;
    private provider: providers.BaseProvider;

    constructor() {
        this.contract = new OpnCashFactory().attach(AppConfig.blockchain.contract);
        this.provider = new providers.JsonRpcProvider(AppConfig.blockchain.rpcUrl, AppConfig.blockchain.chainId);
    }

    async sendTransaction(from: string, to: string, amount: number, options?: Overrides) {
        try {
            this.contract = this.contract.connect(new Wallet(from, this.provider));
            const tx = await this.contract.transfer(to, utils.parseEther(String(amount)), { ...options, gasPrice: 0 });
            const result = await tx.wait();
            return result.transactionHash;
        } catch (err) {
            if (err.code === 'UNPREDICTABLE_GAS_LIMIT') {
                throw new HttpException('Unsufficient amount.', HttpStatus.BAD_REQUEST);
            }
            throw new HttpException(err, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}

My question is how to resolve this issue so the test case have access to AppConfig

Andon Mitev
  • 1,354
  • 1
  • 11
  • 30
  • Can you please also write about how you've imported the `src/config/app.config` in the `user.service.ts` file? – Ehsan Dec 16 '20 at 10:39
  • import { AppConfig } from 'src/config/app.config'; at the top of the file which is object for example: AppConfig.db.name - returns a string with the name of the db and is not passed ot DI or something else it's accessed directly in the service like: AppConfig.User.Secret – Andon Mitev Dec 16 '20 at 10:41
  • By the look of it, you're not referencing a project file as the path does not include `./` at its beginning. Not sure if the reference should be started from `src` folder, which all depends on your project's folder & file organisation. A poosiuble solution can be: `import { AppConfig } from './src/config/app.config';` – Ehsan Dec 16 '20 at 13:35

0 Answers0