#why my test is not working as expected

13 messages · Page 1 of 1 (latest)

dusk flint
#

Hello

Do you know why this test is not passing ?

`import { ConfigService } from '@nestjs/config';
import { Test, TestingModule } from '@nestjs/testing';
import { CardanoService } from './cardano.service';

describe('CardanoService', () => {
let service: CardanoService;
let config: ConfigService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
CardanoService,
{
provide: ConfigService,
useValue: {
get: jest.fn((key: string) => {
if (key === 'cardano.cardanoWalletEndpoint') {
return 'http://localhost:8090/v2';
}
return null;
})
},
},
],
}).compile();

config = module.get<ConfigService>(ConfigService);
service = module.get<CardanoService>(CardanoService);

});

it("should not create a walletServer if there's no cardanoWalletEndpoint key", async () => {
jest.spyOn(config, 'get').mockReturnValueOnce(undefined);
expect(service.walletServer).toBeUndefined();
});
});`

and here is CardanoService:

`import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import {
WalletServer,
Seed,
ApiNetworkInformation,
ShelleyWallet,
} from 'cardano-wallet-js';
import { CreateCardanoWalletDto } from '@apolonsoft/api-interfaces';
@Injectable()
export class CardanoService {
private logger: Logger = new Logger(CardanoService.name);
walletServer: WalletServer;

constructor(private readonly configService: ConfigService) {
const cardanoWalletEndpoint = this.configService.get<string>(
'cardano.cardanoWalletEndpoint'
);
if(cardanoWalletEndpoint){
this.walletServer = WalletServer.init(cardanoWalletEndpoint);
}
this.logger.log('Cardano Wallet Server Initialized');
}
}
`

neat dove
#

What's the failure you're getting? That'll be easier than us trying to look at the test and figure it out ourselves

dusk flint
# neat dove What's the failure you're getting? That'll be easier than us trying to look at t...

`CardanoService › should not create a walletServer if there's no cardanoWalletEndpoint key

expect(received).toBeUndefined()

Received: {"config": {"basePath": "http://localhost:8090/v2"}, "networkApi": {"axios": [Function wrap], "basePath": "http://localhost:8090/v2", "configuration": {"basePath": "http://localhost:8090/v2"}}, "proxyApi": {"axios": [Function wrap], "basePath": "http://localhost:8090/v2", "configuration": {"basePath": "http://localhost:8090/v2"}}, "settingsApi": {"axios": [Function wrap], "basePath": "http://localhost:8090/v2", "configuration": {"basePath": "http://localhost:8090/v2"}}, "stakePoolsApi": {"axios": [Function wrap], "basePath": "http://localhost:8090/v2", "configuration": {"basePath": "http://localhost:8090/v2"}}, "url": "http://localhost:8090/v2", "walletsApi": {"axios": 

[Function wrap], "basePath": "http://localhost:8090/v2", "configuration": {"basePath": "http://localhost:8090/v2"}}}`

neat dove
#

Why do you expect the walletServer to be undefined?

dusk flint
#

in case of missing environment variable

neat dove
#

But that's not what your test is set up for, right? You have a ConfigService mock that returns a value for cardano.cardanoWalletEndpoint, which then gets validated as truthy (it exists) and so this.walletServer = WalletServer.init(cardano.cardanoWalletEndpoint)

dusk flint
#

somehow I need ConfigService to have key cardanoWalletEndpoint =undefined for this test

#

am i doing wrong?

neat dove
#

That won't easily be possible with how your test is set up. For that test specifically I would just write something super simple like

it('should have an undefined walletServer', () => {
  const service = new CardanoService({ get: () => undefined });
  expect(service.walletServer).toBeUndefined();
});
#

Otherwise, you're gonna have to re-write the configMock so that you can change it and you won't really be able to do that if it's being used in the beforeEach, so it's best to just write the manual case out like this

dusk flint
#

Thank You for your Help )

dusk flint
#

it('should have an undefined walletServer', () => { const cardanoService = new CardanoService(new ConfigService({get: () => undefined})); expect(cardanoService.walletServer).toBeUndefined(); });

#

it is working