this is my first time i use Jest for testing so when I tried to run my unit test it fail because the import of communicationManagerService (actualy it call the coreManager.MessageSubject which is not used in my OutCallManager class)
this is my class :
import {Inject, Injectable} from '@nestjs/common';
import {CommunicationManagerService} from 'package/communication-manager';
import {ManagerLogger} from '../../common/logger';
@Injectable()
export class OutCallManager extends AbstractManager implements IOutCallManager {
readonly className = 'OutCallManager';
readonly logger = new ManagerLogger(this.className);
@Inject()
private readonly communicationManagerService: CommunicationManagerService;
constructor(private context: ContextManagerService, private coreManagerService: CoreManagerService) {
super(ServiceName.OutCall);
}
sendEmail(param: SendEmailInput): Observable<MessageHistoriqueContact> {
const designation$ = param.clientId
? this.getClientDesignation(param.clientId, param.clientDesignation)
: of(undefined);
const communication$ = this.communicationManagerService.envoyerMail({
to: param.emailAdress
});
return forkJoin([designation$, communication$]).pipe(
switchMap(([designation, _com]) => {
const historyEntry: MessageHistoriqueContact = {...};
return this.historiqueContactService.postMessage(historyEntry);
}),
catchError((error) => this.throwError(error, 'sendEmail'))
);
}
}
this is my test class :
import {Test} from '@nestjs/testing';
import {OutCallManager} from './out-call.manager';
import { CommunicationManagerService } from 'package/communication-manager';
describe('OutCallManagerService', () => {
let service: OutCallManager;
const communicationManagerServiceMock = {};
const coreManagerServiceMock = {};
beforeEach(async () => {
const moduleRef = await Test.createTestingModule({
providers: [
OutCallManager,
{
provide: CommunicationManagerService,
useValue: communicationManagerServiceMock,
},
{
provide: CoreManagerService,
useValue: coreManagerServiceMock,
},
]
}).compile();
service = moduleRef.get<OutCallManager>(OutCallManager);
});
describe('sendEmail', () => {
it('should send email ', () => {
const spyOnGetService = jest
.spyOn(service, 'getService')
.mockReturnValueOnce(histoContact)
.mockReturnValueOnce(contacterSocle);
const spyOnPostMessage = jest.spyOn(histoContact, 'postMessage').mockImplementation(() => of());
const spyOnEnvoyerMail = jest.spyOn(contacterSocle, 'envoyerMail');
const spyOnGetClientDesignation = jest.spyOn(service, 'getClientDesignation').mockReturnValue(of('designation'));
const param: SendEmailInput = {
emailAdress: '',
clientId: '111111'
};
service.sendEmail(param).subscribe(() => {
expect(spyOnGetService).toHaveBeenCalledTimes(2);
expect(spyOnGetService).toHaveBeenCalledWith(SocfonMysysServiceName.HistoriqueContact);
expect(spyOnGetService).toHaveBeenCalledWith(ServiceNameSocle.Communication);
expect(spyOnPostMessage).toBeCalledTimes(1);
});
});}
the error is TypeError: Test suite failed to run
TypeError: coreManager.MessageSubject is not a function
33 | IParamCreerEMailBandeauWOX
34 | } from package/communication-common';
> 35 | import {CommunicationManagerService} from 'package/communication-manager';
| ^
36 | import {ContextManagerService, CoreManagerService} from 'package/core-manager';
i tried to mock coreManager.MessageSubject but it is not the solution