I encountered a problem while trying to write a unit test for my Electron app using Jasmine and Angular 6. The issue arises from the fact that I have a service which is not required to be tested in the specific test scenario of another service. To handle this, I attempted to mock the first service in the following way:
import { Injectable } from '@angular/core';
import { TestService } from '../../services/test/test.service';
@Injectable()
export class TestServiceMock extends TestService {
private somePath: string;
public isFileExistent(path: string): boolean {
return path === '\\some\\kind\\of\\path\\some.json' ? true : false;
}
}
However, Visual Studio Code displayed the error message:
[ts] Class 'TestServiceMock' incorrectly extends base class 'TestService'.
Types have separate declarations of a private property 'somePath'.
I believe this error might be occurring due to a perceived difference in the "somePath" member between the "TestService" and the "TestServiceMock", although I can't seem to identify any actual distinction (in my understanding). This is the implementation of the original "TestService":
import {Injectable} from '@angular/core';
import {ElectronService} from '../electron/electron.service';
@Injectable()
export class TestService {
private somePath: string;
constructor(private electron: ElectronService) {
this.somePath = this.electron.remote.app.getAppPath();
}
public isFileExistent(path: string): boolean {
return // some boolean after a lot of operations not needed here
}
}
Furthermore, when attempting to run the test, it resulted in the error:
[ts] Type 'typeof TestServiceMock' cannot be converted to type 'TestService'.
Property 'somePath' is missing in type 'typeof TestServiceMock'.
Here's the relevant test code snippet:
import { RealService } from './real.service';
import { TestServiceMock } from '../../response-models/test/test.service.mock';
import { TestService } from '../../services/test/test.service';
describe('RealService:', () => {
let realservice: RealService;
beforeEach(() => {
realservice = new RealService(TestServiceMock as TestService);
});
it('should be available:', () => {
expect(realservice).toBeDefined();
});
});
I would appreciate your assistance with resolving this issue. Thank you in advance!